0

Right now I have this page where I use foreach to fill the page. I then have foreach inside of that to get another table with rows on the specific ID. The problem being anything under the nested foreach doesn't fill.

Table

enter image description here

Example:

<!-- Get The Person. Results in ONE person. -->
<?
$list = $db2->prepare(" SELECT * FROM Table WHERE ID = ? ");
$list->execute(array($id));
$data = $list->fetchAll();
foreach ($data as $row) :?>

<div><?=$row["name"]?></div>

<!-- Get The Colors. Gets as many results as there are for that ID. -->
<?
$list2 = $db2->prepare(" SELECT * FROM AnotherTable WHERE ID = ? ");
$list2->execute(array($id));
$data2 = $list2->fetchAll();
foreach ($data2 as $row2) :?>

<div><?=$row2["color"]?></div>

<?endforeach?>

<!-- Doesn't show up on the page. -->
<div><?=$row["food"]?></div>

<?endforeach?>
Kyle Dunne
  • 231
  • 1
  • 10
  • 1
    You should use JOIN in your query – Ravi Hirani Jun 06 '16 at 05:21
  • Stop using shot-tag like , short-tag ...?> is deprecated now start using . – AkshayP Jun 06 '16 at 05:22
  • 3
    in another foreach you have set $data instead of $data2 – Kaushal shah Jun 06 '16 at 05:22
  • Ravi Hirani is right above: using a `JOIN` you could implement the same using only a single query. That makes things faster, especially for bigger data sets and also easier to read, understand and debug. – arkascha Jun 06 '16 at 05:24
  • @arkascha a `JOIN` would work with a table like what I just added to the example? Cause right now if I used a join with my current code it would throw out 2 results instead of just one. – Kyle Dunne Jun 06 '16 at 05:31
  • @Kaushalshah that was it. Thanks! I had more $data than I noticed. – Kyle Dunne Jun 06 '16 at 05:44
  • Why do you need the first `foreach`? Isn't `ID` a unique key of the first table, so the query just returns 1 row? – Barmar Jun 06 '16 at 05:54
  • @Barmar It isn't always just 1 row for this specific use I'm on, but I figured it would be more clear what I was asking if I framed it like that. – Kyle Dunne Jun 06 '16 at 14:31

2 Answers2

0

I think you are using the same $id on both tables which is what is causing the problem. If it is a child table and Table_ID is the foreign key, maybe

" SELECT * FROM AnotherTable WHERE Table_ID = ? "

or you could use join and do

"SELECT * FROM Table INNER JOIN AnotherTable ON Table.ID = AnotherTable.Table_ID WHERE ID = ? "

Am assuming Table_ID is your foreign key there.

phreakv6
  • 2,135
  • 1
  • 9
  • 11
  • I know about joins I just don't know what to do with one that would work on normalized tables. – Kyle Dunne Jun 06 '16 at 05:41
  • Going back to your original problem, there is no reason why your nested foreach shouldn't work. Did you try debugging the query that is being executed? And also the return value? (print_r($data2))? – phreakv6 Jun 06 '16 at 05:43
0

As suggested in comment,

You should use JOIN in your query to avoid two foreach loop and get result in single query.

Write your query as below,

SELECT * FROM Table1 JOIN Table2 USING (ID) WHERE ID = ?;

same as

SELECT * FROM Table1 JOIN Table2 ON Table1.ID = Table2.ID WHERE Table1.ID = ?

Note:- To differentiate column names, you can use alias in column name.

Hope it will help you :-)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42