PHPExcel str_replace - hyperlink

i want to convert my excel hyperlink's path to open links with PHPExcel.
$links = $objPHPExcel->getActiveSheet()->getHyperlinkCollection();
This method will return an array of hyperlink objects, indexed by cell address; and you could then use array_filter() with an appropriate callback and the ARRAY_FILTER_USE_KEY flag set to extract those within a specific range.
my var_dump($links); output :
But i dont know how to loop the array of objects with array_filter() function..
I try :
$test = str_replace($links, "file", "mnt");
var_dump($test);
and i got the error above.. Any ideas please ?

The collection is an array of objects, indexed by cell address; and the PHPExcel_Cell_Hyperlink object has a set of documented methods for accessing and setting its data:
foreach($links as $cellAddress => $link) {
// get the URL from the PHPExcel_Cell_Hyperlink object
$url = $link->getUrl();
// change the URL however you want here
$url = str_replace($url, "file", "mnt");
// Set the new value for the link
$link->setUrl($url);
}
If you want to modify just those URLs for cells in column N, then you can wrap them in an if test:
foreach($links as $cellAddress => $link) {
// Test for column N
sscanf($cellAddress, '%[A-Z]%d', $column, $row);
if ($column == 'N') {
// get the URL from the PHPExcel_Cell_Hyperlink object
$url = $link->getUrl();
// change the URL however you want here
$url = str_replace($url, "file", "mnt");
// Set the new value for the link
$link->setUrl($url);
}
}

Related

Array is empty, even though it is set in attached

I have the following code:
Grid get _gridA => $['myGridA'];
Grid get _gridB => $['myGridB'];
Grid get _gridC => $['myGridC'];
List<Grid> _grids = [];
Grid _selectedGrid = null;
attached(){
// Sets array and sets the defaul selected Grid.
_grids = [_gridA, _gridB, _gridC];
_selectedGrid = _grids[0];
}
onTabChange(index, _){
//crashes here, because at this point, _grids is still [] and not [a,b,c]
_selectedGrid = _grids[index];
}
Is there a way to resolve this? set doesnt do anything either.
When trying to instantiate it at the top where i defined it as [], I would get the error that I cant have non-static items in my array.

Validator\Db\RecordExists with multiple columns

ZF2 docs show the following example in terms of using Db\RecordExists validator with multiple columns.
$email = 'user#example.com';
$clause = $dbAdapter->quoteIdentifier('email') . ' = ' . $dbAdapter->quoteValue($email);
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'users',
'field' => 'username',
'adapter' => $dbAdapter,
'exclude' => $clause
)
);
if ($validator->isValid($username)) {
// username appears to be valid
} else {
// username is invalid; print the reason
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
I’ve tried this using my own Select object containing a more complex where condition. However, isValid() must be called with a value parameter.
In the example above $username is passed to isValid(). But there seems to be no according field definition.
I tried calling isValid() with an empty string, but this does not produce the desired result, since Zend\Validator\Db\AbstractDb::query() always adds the value to the statement:
$parameters = $statement->getParameterContainer();
$parameters['where1'] = $value;
If I remove the seconds line above, my validator produces the expected results.
Can someone elaborate on how to use RecordExists with the where conditions in my custom Select object? And only those?
The best way to do this is probably by making your own validator that extends one of Zend Framework's, because it doesn't seem like the (No)RecordExists classes were meant to handle multiple fields (I'd be happy to be proven wrong, because it'd be easier if they did).
Since, as you discovered, $parameters['where1'] is overridden with $value, you can deal with this by making sure $value represents what the value of the first where should be. In the case of using a custom $select, $value will replace the value in the first where clause.
Here's a hacky example of using RecordExists with a custom select and multiple where conditions:
$select = new Select();
$select->from('some_table')
->where->equalTo('first_field', 'value1') // this gets overridden
->and->equalTo('second_field', 'value2')
;
$validator = new RecordExists($select);
$validator->setAdapter($someAdapter);
// this overrides value1, but since isValid requires a string,
// the redundantly supplied value allows it to work as expected
$validator->isValid('value1');
The above produces the following query:
SELECT `some_table`.* FROM `some_table` WHERE `first_field` = 'value1' AND `second_field` = 'value2'
...which results in isValid returning true if there was a result.

Cannot query a second result set without looping through the first result set

I am having an issue with ZF2 trying to use the table gateways and getting result sets:
I am trying to query two result sets (from two different tables/two different gateways) and send them to the view to be iterated through and placed on the screen.
(Simplified example):
function viewAction() {
$table1 = $this->getServiceLocator()->get('Model\Table\Table1');
$table2 = $this->getServiceLocator()->get('Model\Table\Table2');
return new ViewModel([
'table1' => $table1->fetchAll(),
'table2' => $table2->fetchAll()
]);
}
With the Model\Table\Table1 and Model\Table\Table2 having a fetch all:
public function fetchAll() {
return $this->tableGateway->select();
}
Then in my view:
...
<?php
foreach($table1 as $row) {
echo "<tr><td>{$row['col1']}</td><td>{$row['col2']}</td></tr>";
}
?>
...
<?php
foreach($table2 as $row) {
echo "<tr><td>{$row['col1']}</td><td>{$row['col2']}</td></tr>";
}
?>
...
The problem is, $table1 will have no data when looping. However, if I instead do something like this (in the controller, instead of passing the result set to the view, passing $results1 and $results2 to the view):
$fetchAll = $table1->fetchAll();
$results1 = [];
foreach($fetchAll as $row) {
$results1[] = $row;
}
$fetchAll = $table2->fetchAll();
$results2 = [];
foreach($fetchAll as $row) {
$results2[] = $row;
}
Then all works fine. I don't want to have to loop through the same set of data twice. So why does ZF2 prevent me from using two different ResultSets before all the data in a ResultSet has been accessed?
Since I was querying to different record sets one after the other, the database was still waiting for action to take place on the first query. I could only have one action record set at a time.
There first solution was what I presented in the question, get a record set and loop through all the rows that are available before trying to query again. This frees the record set as we have all the data.
The second solution (the one I have adapted), is to enable Multiple Active Record Sets (MARS) for the database connection.
I was able to do this by adding MARS_Connection=yes to the dsn connection string for MSSQL.

Create json object in php foreach loop

I have the following in php:
$follow=explode(" ",$_SESSION['Following']); //create array from the string stored in session variable
foreach($follow as $val) {
$show = $val;
//my query
$result=mysqli_query($dbc,$query);
WHILE ($rows = mysqli_fetch_assoc($result)) {
//$array[]= $rows; // tried this
//$array=json_encode($rows); //tried this
//array_push($array,$rows); // tried this
}
$json_array=json_encode($array);
echo $json_array;
If I take a single pass through the foreach loop the json object looks like this:
[{key:value}....], which can be parsed in my javascript.
However, with multiple passes in the foreach I am getting multiple arrays
within the object ,like this: [{key:value}][{key:value}]..... which results in the following
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data, which I guess are the []'s inside the object. How can I create the json object in the foreach loop to fix this?
Fixed it. I was echoing $json_array inside the foreach loop.

How would I find the text of a node that has a specific value for an attribute in groovy?

I'm using XMLSlurper. My code is below (but does not work). The problem is that it fails when it hits a node that does not have the attribute "id". How do I account for this?
//Parse XML
def page = new XmlSlurper(false,false).parseText(xml)
//Now save the value of the proper node to a property (this fails)
properties[ "finalValue" ] = page.find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
I just need to account for nodes without "id" attribute so it doesn't fail. How do I do that?
You could alternatively use the GPath notation, and check if "#id" is empty first.
The following code snippet finds the last element (since the id attribute is "B" and the value is also "bizz", it prints out "bizz" and "B").
def xml = new XmlSlurper().parseText("<foo><bar>bizz</bar><bar id='A'>bazz</bar><bar id='B'>bizz</bar></foo>")
def x = xml.children().find{!it.#id.isEmpty() && it.text()=="bizz"}
println x
println x.#id
Apprently I can get it to work when I simply use depthFirst. So:
properties[ "finalValue" ] = page.depthFirst().find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};

Resources