Page 1 of 1

Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 7:11 pm
by Vestax159
Forgive me im fairly new to php and not sure what this would be called or I would of searched for it more thoroughly. Im using a while loop (shown below) to grab each row of my database. Later on I want to use certain values from the rows as my name / link for a css menu. So what I need is something to say Temp_1 = $row then for the next row increment the variable to Temp_2 = $row. Of course if you guys / gals can think of a better way feel free to share.

Code: Select all

 
while ($row = mysqli_fetch_assoc($result))
{
    extract($row);
 
}
 

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 7:15 pm
by califdon
I should think you would want to read the values into an array, then you could assign them to whatever you wanted. You do realize, I presume, that $row is an array, itself, and that extract($row) creates variables named for the fields in the row, so you would never have $Temp_2=$row.

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 7:38 pm
by Weiry
As califdon said, each row is an array of its own.

Code: Select all

while ($row = mysqli_fetch_assoc($result)){
    $yourArrayData[] = $row;  // Read row into new array
}
Then access each row as:

Code: Select all

 
foreach($yourArrayData as $row){
    print $row['tableFieldName'];  // Print field from the table row.
}

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 8:40 pm
by Vestax159
Ok I think I'm following this but let me make sure as I don't want to just copy / paste I want to understand php.

Code: Select all

 
while ($row = mysqli_fetch_assoc($result)){
    $yourArrayData[] = $row;  // Read row into new array
}
 
This grabs your results and assigns it to $row then it assigns it into $yourArrayData[] which rather then override the information will insert the new row information into $yourArrayData.

Code: Select all

 
foreach($yourArrayData as $row){
    print $row['tableFieldName'];  // Print field from the table row.
}
 
This I'm a little lost on but from reading foreach it seems like it will place the first row of information in $yourArrayData back into $row. Then it changes the variable of $row to $row1 for the next bit of information?

Sorry, as I said pretty new to PHP but been trying to read what I can. Just wondering, why did you use print rather then echo?

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 8:50 pm
by Weiry
$row is simply a variable name.

Think of it like this,
$row before it is added to $yourArrayData is a list of items in your database's table entry..

Code: Select all

`id`   `name`
1    Jim
2    Joe
3    John
so $row refer's to say "1, Jim"

The first part of that code inserts that particular row into a new array. So your array will look like this with 3 entries.

Code: Select all

$yourArrayData = array(
    array("id" => 1, "name" => "Jim"),
    array("id" => 2, "name" => "Joe"),
    array("id" => 3, "name" => "John")
);
The foreach loop basically runs through each entry that you have in your array, and you can then refer to each field by its name.

Code: Select all

foreach($yourArrayData as $row){  // $row in this case, refer's to the current row of data inside the $yourArrayData array.
    print "ID: ".$row['id']." - Name: ".$row['name'];  // Print field from the table row.
}

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 8:53 pm
by AbraCadaver
You have it I think, but to clarify: The first loop assigns the array fetched from the db into $row and then you assign the value of $row to $yourArrayData[0] and the next row to $yourArrayData[1], etc. So you have an array of all the rows from the db.

Your next loop loops through $yourArrayData and assigns the value of $yourArrayData[0] to $row and then you print it, and then assigns the value of $yourArrayData[1] to $row and prints it etc.

Seems like that's what you said, but just making sure.

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 9:25 pm
by Vestax159
Hmm.. I don't think I described my problem enough so let me explain further.

Code: Select all

 
// Grab each row
while ($row = mysqli_fetch_assoc($result))
{
    extract($row);
    
}
 
?>
 
<!-- Menu  -->
<div id="menu-container">
<div id="menu">
    <ul>
                <!-- 
                replace # with link based on $categories_id and Main with $categories_name 
                -->
        <li><a href="#" class="top_parent">Main</a>
        <ul>
            <li><a href="#" class="parent">Sub</a>
                <ul>
                <li><a href="#">Sub 2</a></li>
                <li><a href="#">Sub 2</a></li>
                <li><a href="#">Sub 2</a></li>
                <li><a href="#">Sub 2</a></li>
                </ul>
            </li>
        
</div>  <!-- end the menuh-container div -->  
</div>  <!-- end the menuh div --> 
 
 
Ok so right now if I fill in each each href or name with $categories_id or $categories_name it will only display the last rows value from the while loop. An if statement inside of the while loop might be able to produce the desired results it would leave the code rather messy.

Re: Assign a unique variable for each while loop result

Posted: Fri Feb 19, 2010 10:14 pm
by Weiry
This is why you would create a new array of results inside the while loop..

Then when it comes time to print the stuff out, just use a foreach loop on the array with the data in it...

Code: Select all

<?php
while ($categoryData = mysqli_fetch_assoc($result)){
    $categoryArray [] = $categoryData;  // Read row into new array
}
?>
 
// blah with the rest of the code up till you print the data out.
 
<li><a href="#" class="parent">Sub</a>
     <ul>
     <?php foreach($categoryArray as $category){   // for each category entered in the category array
          print "<li><a href='#{$category['id']}'>{$category['name']}</a></li>";   // print out a new line per category.
              } ?>
    </ul>
</li>