kNair
kNair

Reputation: 25

How to loop inside a while loop

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 ?

Upvotes: 0

Views: 130

Answers (3)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

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

Upvotes: 0

Sanket
Sanket

Reputation: 36

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.

Upvotes: 0

iWantSimpleLife
iWantSimpleLife

Reputation: 1954

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.

Upvotes: 1

Related Questions