I have the following format:
{ "_id" : { "$oid" : "61f41a529210000060005487" }, "number" : "3", "name" : "Honduras", "description" : "1500m height farming, price per 200gr", "price" : 7, "stock" : 10, "stars" : "Rated with 4.0 stars by users." }
I need to parse a few entries like the above in PHP. I am using foreach for that purpose. Everything except {"_id": {"$oid": ... }} field seems to be parsed correctly. I get the following error:
Fatal error: Uncaught TypeError: Cannot access offset of type string on string
PHP script:
$products = json_encode($cursor['products']);
$products = json_decode($products, true) ;
foreach ($products as $product) {
$stringID = (string)$product['_id'];
echo '<tr>';
echo '<td>' . $stringID . '</td>';
echo '<td>' . $product['number'] . '</td>';
echo '<td>' . $product['name'] . '</td>';
echo '<td>' . $product['description'] . '</td>';
echo '<td>' . $product['price'] . '</td>';
echo '<td>' . $product['stock'] . '</td>';
echo '<td>' . $product['stars'] . '</td>';
echo '</tr>';
}
I have tried many solutions can't parse it though. Any suggestions?
$product is a string, not an object. You should json_decode($product) before to access to properties.
foreach ($products as $product)
{
$product = json_decode($product); // << decode here
$stringID = $product['_id']['$oid']; // << access to ID
// echo row :
echo '<tr>';
echo '<td>' . $stringID . '</td>';
echo '<td>' . $product['number'] . '</td>';
echo '<td>' . $product['name'] . '</td>';
echo '<td>' . $product['description'] . '</td>';
echo '<td>' . $product['price'] . '</td>';
echo '<td>' . $product['stock'] . '</td>';
echo '<td>' . $product['stars'] . '</td>';
echo '</tr>';
}
foreach($objPHPExcel->getWorksheetIterator() as $worksheet)
{
echo '<table border ="1px" width ="50%" float:left overflow:auto>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$y =0;
foreach ($cellIterator as $cell) {
$promptData[$x][$y] = $cell->getValue();
$cellIndex = $cell->getCoordinate();
$promptData[$x][$y]['Index'] = $cellIndex;
echo '<td>' . $promptData[$x][$y] . '</td>' .'<td>' . $cell->getCalculatedValue() . '</td>' . "\n";
$y++;
}
$x++;
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
}
I want to store index of the cell in the 2d array I am using
$promptData[$x][$y]['Index'] = $cellIndex;
But it does not work can anyone help.
The problem is you're defining the value $prompt[$x][$y] first as a value, then as an array:
$promptData[$x][$y] = $cell->getValue();
$cellIndex = $cell->getCoordinate();
$promptData[$x][$y]['Index'] = $cellIndex;
echo '<td>' . $promptData[$x][$y] . '</td>' .'<td>' . $cell->getCalculatedValue() . '</td>' . "\n";
Try assigning the value to an array element for the cell, then echoing just that array element rather than the whole array:
$promptData[$x][$y] = [
'Value' => $cell->getValue(),
'Index' => $cell->getCoordinate()
];
echo '<td>' . $promptData[$x][$y]['Value'] . '</td>' .'<td>' . $cell->getCalculatedValue() . '</td>' . "\n";
How can I properly do the the numbering of the results? What i did was
<?php
if($results)
{
$x = 1;
foreach ($results as $data)
{
echo '<tr>';
echo '<td>' . $x++. '</td>';
echo '<td>' . $data->item_id . '</td>';
echo '<td>' . $data->item_name . '</td>';
echo '<td>' . $data->item_category . '</td>';
echo '<td>' . $data->item_costprice . '</td>';
echo '<td>' . $data->item_retailprice . '</td>';
echo '<td>' . $data->item_tax . '%</td>';
echo '</tr>';
}
}
?>
But it has the same numbering per page.
The numbering starts with 1 because thats the value you assign to it before the loop starts.
Assuming you pass the page number to your controller your could do the following:
$x = count($results) * ($pagenumber-1);//use count or some other type of count method
This will make the $x value 1 on the first page, 31 on the second page, 61 on the third page etc...
Then inside the loop change $x++ to ++$x otherwise the first page will be counted from 0 to 29.
Wondering if someone could help give me a push in the right direction, I am building a search function (php and mysql) which will display search results and highlights keywords that the user has searched for. at the moment I grab the search criteria that the user has entered and query that against the database which works fine to get the desired results. the problem I have is
$highlight = preg_replace("/".$_GET['criteria']."/", "<span class='highlight'>".$_GET['criteria']."</span>", $_row['name']);
This will only highlight a phrase and not individual keywords. so for example if the document was called "Hello world" and the user typed this exactly it would highlight no problem however if the user typed "world hello" it will not highlight anything. I thought it would be a good idea to take the search criteria and use explode and check each word individually but this seems to fail as well. here is my query and how I am displaying results
$sql = "SELECT *
FROM uploaded_documents
WHERE dept_cat = 'procedures'
AND cat =:cat
AND keywords REGEXP :term ";
$result->execute(array(':cat' => $_GET['category'],':term' => $_GET['criteria']));
//display results
while($row = $stmt->fetch()){
$explode_criteria = explode(" ",$_GET['criteria']);
foreach($explode_criteria as $key){
$highlight = preg_replace("/".$key."/", "<span class='highlight'>".$key."</span>", $row['name']);
echo '<td><a target="_blank" href="'.$row['url'].'">'.$highlight.'</a></td>';
echo '<td>'.$row['version'].'</td>';
echo '<td>'.$row['cat'].'</td>';
echo '<td>'.$row['author'].'</td>';
echo '<td>'.$row['added'].'</td>';
echo '<td>'.$row['auth_dept'].'</td>';
echo '<td>';
}
}
For the sake of length I have omitted code here and tried to keep it minimal, I have been trying to base my work on the following post
highlighting search results in php/mysql
I think my first problem is the foreach loop in the while loop duplicating results but I cant think of a way around it.
Thanks in advance
In this block of code:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
foreach ($explode_criteria as $key)
{
$highlight = preg_replace("/" . $key . "/", "<span class='highlight'>" . $key . "</span>", $row['name']);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
The loop is constantly referring to $row['name'], so the replacement is done, but the next time the loop happens it is replacing the next word on the original unmodified $row['name']
I think this should help you:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
$highlight = $row['name']; // capture $row['name'] here
foreach ($explode_criteria as $key)
{
// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace("/" . $key2 . "/", "<span class='highlight'>" . $key . "</span>", $highlight);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
I have this bit of code which loops through an array and echos out the result to the page thus:
while($row = mysqli_fetch_array($result)) {
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $row['media'] . "</td></tr><br />\n";
}
It works just fine, but I was hoping to use an 'if' statement on the $row['media'] because it contains some NULL and some !NULL results.
I wanted to be able to echo a different response a little like:
if ($row['media'] != NULL){
echo 'Nope';
} else {
echo $row['media'];
}
Is this possible in this situation?
Thanks.
use:
if ( is_null( $row['media'] ) ) { ... } else { ... }
The best way to accomplish this is using ternary operators:
while (whatever)
{
echo 'foo'
.($statement ? 'bar' : '')
.'baz';
}
Yeah, you would just end the echo, perform the if statement, and then use another echo to finish the code off. When it is parsed, the HTML will still be usable.
while($row = mysqli_fetch_array($result)) {
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>';
if($row['media'] == NULL) { echo 'Nope'; } else { echo $row['media']}
echo "</td></tr><br />\n";
}
Well, a very simple solution would be to do this...
$media = $row['media'];
if ($row['media'] == NULL)
$media = 'nope';
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' .$row['name']. '</a></td>';
echo '<td>' . $row['provider'] . '</td>' . '<td>' . $media . "</td></tr><br />\n";
Yes, break your echo into two different echos:
echo "<tr><td><a target="_blank" href="'" ; // (etc etc)
if($row['media'] != NULL) {
echo "NOPE";
} else {
echo $row['media'];
}
echo " $row['url'] . '">'; // (etc etc)
The syntax isn't perfect but I'm pretty sure you'll get the idea :)
If I understand your question then this should work just fine:
if(is_null($row['media']) echo($row['media']) else echo('Nope');
you can always just use another variable and set it before your echo statement, then use that variable in your echo statement. if you want a one-liner, you can use shorthand syntax like this:
($row['media'] != null) ? 'Nope' : $row['media']
and insert that where you currently just have $row['media']
Why not do it this way?
while($row = mysqli_fetch_array($result)) {
$media = ($row['media'] != NULL) ? $row['media'] : "Invalid";
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $media . "</td></tr><br />\n";
}
Have the value put into a temp variable before the echo:
$mediaVal = $row['media'];
if ($mediaVal == NULL) $mediaVal = 'Nope';
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' . '<td>' . $row['provider'] . '</td>' . '<td>' . $mediaVal . "</td></tr><br />\n";
You might consider stripping each field out into a dedicated variable incase you would life to process them in a similar manner etc
You can do something like this in your select statement
select
CASE
WHEN media IS NULL THEN 'Nope';
ELSE media
END as media
from
table
where......
read more : link text
Yes you can do that. You might want to break the echo into multiple echos so its a little easier to see whats going on.
Also, you should check over your if statement. Be careful with your conditionals.
if ($row['media'] != NULL) {
echo 'Nope';
} else {
echo $row['media'];
}
That will output 'Nope' if $row['media'] is not null. I assume you want to output 'Nope' if $row['media'] is null. In that case you want to use == instead of !=.
You can put it in one statement:
while ( $row = mysqli_fetch_array($result) ) {
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' . $row['name'] . '</a></td>' .
'<td>' . $row['provider'] . '</td>' .
'<td>' . (is_null($row['media'])?"Invalid Value":$row['media']) . "</td></tr><br />\n";
}
while($row = mysqli_fetch_array($result)) {
echo '<tr><td><a target="_blank" href="' . $row['url'] . '">' .
$row['name'] . '</a></td>' . '<td>' . $row['provider'] .
'</td>' . '<td>' .
($row['media'] == NULL ? 'Not Assigned' : $row['media']).
"</td></tr><br />\n";
}