PHP display data from SQL

Discussion in 'PHP' started by johneva, Feb 12, 2010.

  1. #1
    Hi

    I have some data in a database I want to retrive but could do with some help on how to code it.

    I want to make a table listing manufacturer/make and then list each vehicle/entry for this manufacturer and then go do the same for each manufacturer in the database.

    id, make, model, spec, seven, twelve, eighteen, special, database_filename

    Being the fields in the database.

    Any help would be greatly appriciated.

    Here is what I had made with the intention of just listing the vehicles for now, then further modifying it to be grouped seperatly by make/manufacturer, but I am currently struggling to even get it to just list them together.

    <table border='1' cellpadding='3'>
    <tr>
     <td></td> 
     <td>Make:</td>
     <td>Model:</td>
     <td>7 Months:</td>
     <td>12 Months:</td>
     <td>18 Months:</td>
    </tr>
    
    <?php
    
    ini_set('display_errors', 1); 
    error_reporting(E_ALL); 
    
    $query = "select * from data order by make"; 
    $result = mysql_query($query) or die("Query failed ($query) - " . mysql_error()); 
    if(mysql_num_rows($result)) 
    { 
       while($row = mysql_fetch_assoc($result)) 
       { 
          $id = $row['id'] ;
          $make = $row['make'] ;
          $model = $row['model'] ; 
          $spec = $row['spec'] ; 
          $seven = $row['seven'] ; 
          $twelve = $row['twelve'] ; 
          $eighteen = $row['eighteen'] ;
        
    echo"<tr>"
    echo"<td><img width='90' height='50' src='control_panel/uploaded_files/x' alt='x' /></td>"
    echo"<td>$make</td>" 
    echo"<td>$model</td>"
    echo"<td>&pound;$seven</td>"
    echo"<td>&pound;$twelve</td>"
    echo"<td>&pound;$eighteen</td>"
    echo"</tr>"
    echo"<th>Spec:</th> <td colspan='6'>$spec</td></tr>"
       } 
    } 
    else
    { 
       echo "<p>No matches were found in the database for your query.</p>\n"; 
    } 
    
    ?>
    </table>
    PHP:
     
    Last edited: Feb 12, 2010
    johneva, Feb 12, 2010 IP
  2. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I didn't try it out on my end, but does your code not work? If not, what's it 'NOT' doing. :p

    Btw here's a 'quick' version of how I would handle it. You know PHP alright it seems, so this is drafty.

    $q = 'mysql query to pull categories of manufacturers';
    $result = mysql_query($q);
    
    while ($manufacturer = mysql_fetch_assoc($result)) {
     $manu = $manufacturer['name'];
     $car_q = "GET * FROM CARS WHERE MANUFACTURUR='".$manu."'";
       $car_query = mysql_query($car_q);
       
        while($car = mysql_fetch_assoc($car_query)) {
           // print out all info about car
           //  $car['name'];
           //  $car['model'];
           //  $car['colors'];
           // and so forth
         }
    
    } 
    PHP:
     
    ProtegeSales, Feb 12, 2010 IP
  3. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #3
    Yeah my code was not working due to me missing the ; after each echo line doe!!!

    I also just had to order them by make/manufacturer so they were in the right order, I now need to figure out how to add a row in as a header for each group on make/manufacturer now and its all ready I think.

    I am going to have a play with your code now and see if I can get it to work the way I want if that wont work then will have to think how to adjust my current coding to add the manufacturer header for each group.

    Cheers

    current code
    
    <?php
    
    ini_set('display_errors', 1); 
    error_reporting(E_ALL); 
    
    $query = "select * from data order by make"; 
    $result = mysql_query($query) or die("Query failed ($query) - " . mysql_error()); 
    if(mysql_num_rows($result)) 
    { 
       while($row = mysql_fetch_assoc($result)) 
       { 
          $id = $row['id'] ;
          $make = $row['make'] ;
          $model = $row['model'] ; 
          $spec = $row['spec'] ; 
          $seven = $row['seven'] ; 
          $twelve = $row['twelve'] ; 
          $eighteen = $row['eighteen'] ;
          $database_filename = $row['database_filename'];
        
    echo"<tr>";
    echo"<td><img width='90' height='50' src='control_panel/uploaded_files/$database_filename' alt='$database_filename' /></td>";
    echo"<td>$make</td>";
    echo"<td>$model</td>";
    echo"<td>&pound;$seven</td>";
    echo"<td>&pound;$twelve</td>";
    echo"<td>&pound;$eighteen</td>";
    echo"</tr>";
    echo"<th>Spec:</th> <td colspan='6'>$spec</td></tr>";
       } 
    } 
    else
    { 
       echo "<p>No matches were found in the database for your query.</p>\n"; 
    } 
    
    ?>
    
    PHP:
     
    johneva, Feb 13, 2010 IP
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    Got it working exactly the way I needed it after doing it the way you said to do it cheers mate :)
     
    johneva, Feb 13, 2010 IP