CodeMama Posted July 16, 2009 Share Posted July 16, 2009 I am outputting data on a page, np there.. some of the datafields have multiple entries like the fields name and address do not, but the fields violations may or may not have more than one entry for a record so on my page I just want it to display like this: name address violation 1 violation 2 etc without echo-ing the name and address again...currently that is what it does...here is my code: if (!empty($_GET['name'])) { $name = $_GET['name']; $sql = "SELECT name, address, inDate, inType, notes, critical, cviolations, noncritical FROM restaurants WHERE name = '$name'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo ($row['name']),'<br>'; echo ($row['address']),'<br>'; echo ($row['inDate']),'<br>'; echo ($row['inType']),'<br>'; echo ($row['notes']),'<br>'; echo ($row['critical']),'<br>' ; echo ($row['cviolations']),'<br>'; } } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 So, the person can have more then one entry in the table? Really you should have a separate tables: restaurants and violations. If you can't use separate tables for some reason, what is the commonality between rows for a restaurant? name? You can sort by name, then when echoing, only echo the name, address, etc when the name changes inside your loop. Quote Link to comment Share on other sites More sharing options...
CodeMama Posted July 16, 2009 Author Share Posted July 16, 2009 It's restaurant inspections, so the name and addresses are the same but there can be several entries on different dates as per inspections...I think what I am needing to do is a foreach loop with an array but not sure how..got the book in my lap and here is my code now but I am getting "invalid arguement error" <?php if (!empty($_GET['name'])) { $name = $_GET['name']; $sql = "SELECT name, address, inDate, inType, notes, critical, cviolations, noncritical FROM restaurants WHERE name = '$name'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo ($row['name']),'<br>'; echo ($row['address']),'<br>'; foreach(($row['inDate']) as $inDate) echo ($row['inDate']),'<br>'; foreach(($row['inType']) as $inType) echo ($row['inType']),'<br>'; foreach(($row['notes']) as $notes) echo ($row['notes']),'<br>'; foreach(($row['critical']) as $critical) echo ($row['critical']),'<br>' ; foreach(($row['cviolations']) as $cviolations) and the error: Warning: Invalid argument supplied for foreach() echo ($row['cviolations']),'<br>'; } } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 Oh, so this is only for 1 restaurant at a time? <?php if (!empty($_GET['name'])) { $name = $_GET['name']; $sql = "SELECT name, address, inDate, inType, notes, critical, cviolations, noncritical FROM restaurants WHERE name = '$name'"; $result = mysql_query($sql) or die(mysql_error()); $header = false; while($row = mysql_fetch_assoc($result)){ if(!$header){ echo ($row['name']),'<br>'; echo ($row['address']),'<br>'; $header = true; } echo ($row['inDate']),'<br>'; echo ($row['inType']),'<br>'; echo ($row['notes']),'<br>'; echo ($row['critical']),'<br>' ; echo ($row['cviolations']),'<br>'; } } Quote Link to comment Share on other sites More sharing options...
CodeMama Posted July 16, 2009 Author Share Posted July 16, 2009 it is, say a user wants to check a restaurant inspections they click on the name, it loads a page that will then display the name and address of the restaurant one time but below that list all the various inspections and dates etc.... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 did you try the code i posted? Quote Link to comment Share on other sites More sharing options...
CodeMama Posted July 16, 2009 Author Share Posted July 16, 2009 I did and it works perfect thanks you Rock Rhodesa!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.