overmonk Posted September 4, 2008 Share Posted September 4, 2008 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? Link to comment https://forums.phpfreaks.com/topic/122709-solved-help-with-pulling-data-from-array/ Share on other sites More sharing options...
lanmonkey Posted September 4, 2008 Share Posted September 4, 2008 <?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? Link to comment https://forums.phpfreaks.com/topic/122709-solved-help-with-pulling-data-from-array/#findComment-633666 Share on other sites More sharing options...
lanmonkey Posted September 4, 2008 Share Posted September 4, 2008 My bad, I think in the above code I modified it should be a "while" loop not "foreach" like this: while($i_love_cake = $i) Link to comment https://forums.phpfreaks.com/topic/122709-solved-help-with-pulling-data-from-array/#findComment-633681 Share on other sites More sharing options...
overmonk Posted September 4, 2008 Author Share Posted September 4, 2008 Something *just* like that. Thanks. Link to comment https://forums.phpfreaks.com/topic/122709-solved-help-with-pulling-data-from-array/#findComment-633702 Share on other sites More sharing options...
BlueSkyIS Posted September 4, 2008 Share Posted September 4, 2008 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) Link to comment https://forums.phpfreaks.com/topic/122709-solved-help-with-pulling-data-from-array/#findComment-633708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.