0

I want to display the number of car makes for a particular car type in while loop.

I want something to look like this:

**Car Type 1**
Car name 1
Car name 2

**Car Type 2**
Car name 1
Car name 2
Car name 3

My problem is that,the data displayed for the car make is correct, But it repeats as many times the first while loop.

Like this:

**Car Type 1**
Car name 1
Car name 1
Car name 2
Car name 2

**Car Type 2**
Car name 1
Car name 1
Car name 1
Car name 2
Car name 2
Car name 2
Car name 3
Car name 3
Car name 3

I know it's because i placed the second while loop inside the first one.

How do i loop the car makes for the particular car type if i placed it outside the first while loop ?

My code is :

<?php
if (isset($num) && ($num > 0)) {
    while ($row = mysql_fetch_array($result)) //first while loop
    {
    $carType_id = $row['carType_id'];
    $carType    = $row['carType'];
    echo '<b>' . $carType . '</b><br/>';

    mysql_select_db($database);
    $query_name = "SELECT car_make.carMake_id,car_make.carMake,type_make.carMake_id,type_make.carType_id,car_type.carType_id FROM car_make,car_type,type_make WHERE car_make.carMake_id=type_make.carMake_id AND type_make.carType_id='$carType_id'";
    $result_name = mysql_query($query_name) or die(mysql_error());

    while ($row = mysql_fetch_array($result_name)) //second while loop
    {
        $carMake_id = $row['carMake_id'];
        $carMake = $row['carMake'];
        echo $carMake . '<BR/>';
    }
  }
}
?>

What is the mistake i did, and How can i fix this ?

4
  • 1
    loop inside a ... loop We need to go deeper! Commented Jul 11, 2014 at 6:00
  • I think its the query $query_name. Did you check the output of query which fetches car make? Commented Jul 11, 2014 at 6:05
  • Are your sure your sql query is right Commented Jul 11, 2014 at 6:09
  • Just improve the sql query with group by Car_Name that should solve the issue and you can avoid use of loop Commented Jul 11, 2014 at 6:16

3 Answers 3

1

Of the top of my head, there are 2 ways.

one is where you query out the car type only, loop through each car type to print the type. then after printing each type, do a query for the names under that car type, then loop for each name to print it.

the second way is to query out the type and names together, sorting by type. You will also need to have a variable to store the current type. for every record, just check if the car type has already been printed, if not, print the type and store the current type in the variable. the bring the car name.

Sign up to request clarification or add additional context in comments.

Comments

0

You first need to store all the car-types inside an array.

Then use that array in diffenent loop.

Or the solution given by Sulthan Allaudeen is also perfect.

Comments

0

Here's what you need to do

Note :

  1. Replace the field cartype, carname, mytable with the proper name in your db
  2. mysql is depreciated, So i am using mysqli

[foreach inside the while loop]

Code here :

<?php
$con=mysqli_connect("hostname","username","password","dbname");
//Replace with appropriate connectinos
$sql = $con->query('select cartype, carname from mytable');   
while ($row = $sql->fetch_array(MYSQLI_ASSOC)) {
    $type_make[$row['cartype']][] = $row['carname'];
}

foreach ($type_make as $cartype => $carname) {
    echo "<b>".$cartype . "</b><br>";
    foreach ($carname as $title) {
        echo $title . "<br>";
    }
}
?>

If you need it to display in the Option - Select, You can make this.

foreach ($type_make as $cartype => $carname) {
    <option value="<?php echo $cartype?>"><?php echo $type."</b>"?></option>
    foreach ($carname as $title) {
        <option value="<?php echo $title?>"><?php echo $title."</b>"?></option>
    }
}

More about foreach :

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

PHP Official

Structure of foreach

Object Iteration

W3schools

Demo - For Each

4 Comments

,can you please explain the foreach part in your solution as I'm not very clear as for the foreach ($type_make as $cartype => $carname)...Thanks
Sorted out ...@Sulthan Allaudeen..Thanks a million ..:)
Welcome, i have updated my answer about the foreach
@SulthanAllaudeen I agree with your answer but would also like to know that why isn't his code working. I mean it should also work using while loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.