Jump to content

[SOLVED] Help with pulling data from array


overmonk

Recommended Posts

I'm pretty confused.  I can code this out longhand, but there has to be a more elegant solution.  I could use a fresh set of eyes; I think I'm overthinking this.

 

<?php
include('include/include_nc.php');

$id = "1";

$sql = "SELECT * FROM tbl_recips WHERE userID='".$id."'";
$result = $mysql->query($sql);

$i = $mysql->arrays($result);

$name100 = $i['recip_100'];
$receipt = $i['orderID'];
$od = $i['orderDate'];
$link = "<a href=./files/$receipt.html>Order Placed: $od </a>";
?>

 

Assume there are three records in tbl_recips with userID = 1.  The first three have a value set for tbl_recips.orderDate, the fourth has NULL.  All four have tbl_recips.orderID set as a text string - order_0, order_1, order_2, and order_3.  The above code stores all of this data in an array, but I can't remember how to loop through it.  I re-read the array seciton, but my eyes are glazing over.

 

I need to loop through (while? foreach?) each record in the array, and see if the orderDate is set.  If it is, I need to generate a hyperlink, add a line break, and process the next record.  It would do this for each record with orderDate set - so it would be three hyperlinks.  But I need to make sure it processes all the records, and generates the hyperlinks for all records where orderDate is not NULL, even if there is a record with a NULL value in between the others. 

 

Does that makes sense? 

<?php
include('include/include_nc.php');

$id = "1";

$sql = "SELECT * FROM tbl_recips WHERE userID='".$id."'";
$result = $mysql->query($sql);

$i = $mysql->arrays($result);
foreach($i as $i_love_cake)
{
  $name100 = $i_love_cake['recip_100'];
  $receipt = $i_love_cake['orderID'];
  
  if($i_love_cake['orderDate'] != null)
  {
    $od = $i_love_cake['orderDate'];
    $link = "<a href=./files/$receipt.html>Order Placed: $od </a>";
  }
  
  //do somthing here with yoru new variables

}
?>

 

something like that?

hmmm, maybe not JUST like that....

 

// This is an assignment of $i to $i_love_cake. As an assignment it will always be true.
// while($i_love_cake = $i) // wrong

// You probably mean to use the comparison operator, == instead of assignment, =
while($i_love_cake == $i)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.