Accessing individual parts of php array [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
$data[$row['order_id'] . $row['ship_to_name'] .
$row['shipping_address'] . $row['billing_address'].
$row['total_paid_incl_vat'] . $row['total_paid_excl_vat'].
$row['base_shipping_incl_tax']][] = $row['name'] . ' €' . $row['base_row_total_incl_tax'];
How can I access $row['order_id'] when looping through it like
foreach ($data as $group_title => $groups) {
When using echo $group_title; it just echoes out everything but I would like to access the single values.
Thanks!

Maybe what you are really looking to do is
while($row = mysqli_fetch_assoc($result) and ($counter < $max)){
// create or fix the $row['name']
$row['name'] = ' €' . $row['base_row_total_incl_tax'];
// add the row to the array indexed by `order_id`
$data[$row['order_id']] = $row;
}
Now you can
foreach ($data as $id => $theRow) {
echo "Order id = $id<br>";
echo "The Address is $theRow[shipping_address]<br>";
// etc

Related

image not picking up the path [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
this my code but unable to display image from db in php.i think this is not pick up the path.any one can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
$result->bindParam('. $empid .', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo '<img src="images/".$row["image"]." ">';
echo '<img src="images/".$row["image"]. > ' ;
}
?>
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
// I assume you have set $empid somewhere in the missing code here
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
// also changed these 2 rows to correct the concatenation
echo '<img src="images/"' . $row["image"] . '">';
echo '<img src="images/"' . $row["image"] . '">';
}
?>

How to update a record based on if previous fields are occupied? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there something wrong with my syntax its not executing in my php script? I am not updating all the records i am trying to update where invoice_no is equal to the '$id' from another form but only if pp1_dt, pp1_amt, pp1_ref is empty else move on to pp2_dt,pp2_amt,pp2_ref and so on to 5.
$i=1;
while($i <= 5) {
$pp_sql = "UPDATE Invoices SET pp'$i'_dt = '$pp1_dt', pp'$i'_amt = '$pp1_amt', pp'$i'_ref = '$pp1_ref' where invoice_no='$id' AND (coalesce(pp'$i'_dt, pp'$i'_amt, pp'$i'_ref) is null)";
if($db->exec($pp_sql)) {
$p_num = $i;
}
else {
$i++;
}
}
you can't use your counter variable inside your string, you need to concatenate it
$pp_sql = "UPDATE Invoices SET pp" . $i . "_dt = '$pp1_dt', pp" . $i . "_amt = '$pp1_amt', pp" . $i . "_ref = '$pp1_ref' where invoice_no=" . $id . " AND (coalesce(pp" . $i . "_dt, pp" . $i . "_amt, pp" . $i . "_ref) is null)";
the way you have it written now, it's trying to find columns in your table called pp$i rather than pp1, pp2, etc, which don't exist.
Shenir, if you want to update any record than you should try to use UPDATE instead of INSERT. if you want INSERT & UPDATE with same query than use key for duplicate records.
I suggest you should read this article http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

php sql subgroup list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a simple code which gives me error. Could someone help me to explain what would be the best solution. I am not just interested the solution rather than the explanation as I have been struggling with this type of listing. I have googled it but I could not find a dummy explanation.
Many thanks
<?php
// Run a Query
$result = mysql_query("SELECT * FROM weblinks ORDER BY yeargroup ASC");
$a=-1;
while ($row = mysql_fetch_array($result)) {
if ($row['yeargroup'] == $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
} else {
echo "<ul><li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li></ul>";
}
$a=$row['yeargroup'];
}
?>
fixed the typo error, thanks
This is how it should be
Year 1
Link1
Link2
Link3
Year 2
Link1
Link2
Link3
etc.
$a = null;
while ($row = mysql_fetch_array($result)) {
//check if $a is equal to the current yeargroup
//if not, output the header and open the list
if ($row['yeargroup'] <> $a) {
echo "<h2 class=\"class\">Year ".$row['yeargroup']."</h2>";
echo "<ul>";
}
echo "<li>";
echo "<img src=\"" . $row['img'] . "\"/>";
echo "".$row['webname']." - ".$row['weblink'];
echo "<div class=\"text\">".$row['content']."</div>";
echo "</li>";
//check if $a is equal to the current yeargroup
//if not, close the list and set $a to the yeargroup for the loop
if ($row['yeargroup'] <> $a) {
echo "</ul>";
$a = $row['yeargroup'];
}
}

Start a new column when MySQL result row count is >= 10 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I cannot figure out how to get my code to create a new HTML column for results, in my table, in a while ($row = mysql_fetch_array($result)) { - when the results returned reach 10, I want a new table column to be created in the same HTML table.
How can I do this? The code I'm using is:
while ($row = mysql_fetch_array
($result, MYSQL_ASSOC)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
echo '<tr><td align="left" bgcolor=' . $row_color . '> <b>' . $row['manufacturer'] . '</b>: <a href=view_inventory.php?mdl_key=' . $row['mdl_key'] . '&man_key=' . $row['man_key'] . '&cls_key=' . $row['cls_key'] . '&sub_cls_key=' . $row['sub_cls_key'] . '> ' . $row['model'] . '</a></b></td></tr>';
$row_count++;
}
You should really use the new PDO interface as the mysql extension is deprecated long ago, read this documentation if you have a choice.
Still, with the old extension, simply do this:
$rowCount = mysql_num_rows($result);
if ($rowCount >= 10) {
while ($row = mysql_fetch_array($result)) {
// Do your extra column stuff here.
}
}
else {
while ($row = mysql_fetch_array($result)) {
// Do your normal stuff here.
}
}
// It is a good practise to remove variables after
// loops, this helps releasing memory in large scripts.
unset($rowCount, $row, $result);

Why does my code output each image 8 times? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have fixed my earlier problem, but now the loop produces 8 or more images for each. In one gallery I have one a single image but get it eight times, not ten, not three, what's with the eight loops?
while($row = mysql_fetch_array($result)){
$AID = $row['AID'];
$ThumbFilePath = $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
foreach($row as $cell)
{
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
}
You have a nested loop.
while($row = mysql_fetch_arry($result)){ //once for each row in the database
foreach($row as $cell){ //once for each field in the table
...
}
}
I'm guessing your table has 8 fields. Remove the foreach{}.
8 columns selected in $result
so $row is an array consist of 8 element.
foreach($row as $cell)
foreach will loop 8 times.

Categories