Jump to content

[SOLVED] Array or What is this called I am trying to do


CodeMama

Recommended Posts

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>';
                                                              }
                                                                }

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.

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>';
                                                              }
                                                                }

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>';
                                                              }
                                                                }

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....

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.