tags:

views:

56

answers:

1

Following on from a question earlier today this answer was given to read the data into an array and separate it to print vehicle type and then some data for each vehicle.

<?php
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>

This is almost perfect for what I want to do but I need to print out two columns from the database ie ford and mondeo before going into the second foreach loop. I've tried print $rows['model'] and all the other combinations I can think of but that doesn't work. Any help much appreciated

A: 

I'm a bit confused by your question but the SELECT * in the SQL statement means that every column from the database should be present as a key=>value pair in the $row array. So if you needed another "column," output here into an HTML list element <li>, you just echo (note: not "print") that column name as an array key. So if you needed the type of the car column found in a column with the name "model" you'd do this:

<?php
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
    <li><?php echo $vehicleData['model'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>

EDIT: I'm still unclear on your question, but if every car has the same vehicleType, and you're just looking to grab that once before looping through all the results, I'm guessing this will do it:

<?php
// Set up a SQL query to grab one row
$query_to_grab_only_vehicle_type = "SELECT vehicleType FROM apparatus WHERE 1 LIMIT 0,1";
// Fetch that row and turn it into an array
$vehicle_type_array = mysql_fetch_array(mysql_query($query_to_grab_only_vehicle_type));
// Initialize a variable with the array value that came from the vehicleType column
$vehicle_type = $vehicle_type_array['vehicleType'];
// You could uncomment and use the following to echo out the variable
// echo "Vehicle type: $vehicle_type";

$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
    <li><?php echo $vehicleData['model'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>
editor
Thanks, yes the data is present and I can echo it out as you suggest but that would give me many echo's of model. I need it before the loop so it is only echo'd out once and until $rows as vehicleData it doesn't exist
bsandrabr
It's the while loop that's giving you "many echos," so it sounds like you might want to just initialize the array once. In that case, above the while call this: "$singleRow = mysql_fetch_array($getSQL)"That would just initialize one array, versus initializing the array for each item in the row. You might want to call this in conjunction with a LIMIT 0,1 so that you don't pull any rows you don't need.
editor
The while loop pulls out all the data I need ie ford mondeo, blue hardtop, ford mondeo, red softtop, the answer given gives > ford, blue hardtop, ford, red softop. What I need is ford mondeo, blue hardtop, red softop
bsandrabr
Where do you get "ford mondeo" from... vehicleData['vehicleType']?
editor
sorry I'm confusing the issue by trying to relate it to someone elses question . I think ford mondeo is vehicle type.
bsandrabr
What I actually have is forename surname so the two always go together I can get forename with the above question but not surname which is in a different column
bsandrabr
Well it'd certainly be more helpful if you showed your actual code, because this is getting pretty confusing. If you do a print_r() on whatever your equivalent of $vehicleData is (print_r($vehicleData)) what is the output and which of the columns are you looking to print out (surname and forename, it seems)?
editor
thanks for your patience . I am trying to get forename surname and then data for each. I can only get print r to work in the loop at the moment. this is the first bit:GregoryArray ( [morning] => [afternoon] => [date] => [date2] => 2010-05-03 [fname] => Gregory [lname] => Acosta [customerid] => 228 )
bsandrabr
ok thanks I'll try your answer to my other question
bsandrabr