1

Any help is appreciated. I have an array that is forfetch like this. The reason is to brake down a product in individual arrays. However I can not figure out what statement to put in the while loop so i can loop through each array in $row. initially the statement should be

 while ($row = mysql_fetch_assoc($result)) 

however this was already done to be able to sort the array.

$sorted = array_orderby($newarray, 'volume', SORT_DESC, 'edition', SORT_ASC);


foreach($sorted as $row)
{

    while(  ???????   )

{
    $row = build_items($row);



        $template->assign_block_vars('featured_items', array(
            'ID' => $row['id'],
            'IMAGE' => $row['pict_url'],
            'TITLE' => $row['title'],
            'SUBTITLE' => $row['subtitle'],
            'BUY_NOW' => ($difference < 0) ? '' : $row['buy_now'],
            'B_BOLD' => ($row['bold'] == 'y')
        ));
        $k++;
        $feat_items = true;
    }
}

Just found the answer. Sorry guys im new at PHP.

   foreach($sorted AS $row) {


        $row = build_items($row);

        // time left till the end of this auction 
        $s_difference = time() - $row['starts'];
        $difference = $row['ends'] - time();
        $bgcolour = ($k % 2) ? 'bgcolor="#FFFEEE"' : '';

        $template->assign_block_vars('featured_items', array(
            'ID' => $row['id'],

            'IMAGE' => $row['pict_url'],
            'TITLE' => $row['title'],
            'SUBTITLE' => $row['subtitle'],
            'BUY_NOW' => ($difference < 0) ? '' : $row['buy_now'],
            'BID' => $row['current_bid'],
            'BIDFORM' => $system->print_money($row['current_bid']),
            'TIMELEFT' => FormatTimeLeft($difference),
            'NUMBIDS' => $row['num_bids'],

            'B_BOLD' => ($row['bold'] == 'y')
        ));
        $k++;
        $feat_items = true;

}

2 Answers 2

5
foreach($sorted AS $rows) {
  foreach($rows AS $row) {
  ...
  }
}

or with keys/indices

foreach($sorted AS $key => $rows) {
  foreach($rows AS $index => $row) {
Sign up to request clarification or add additional context in comments.

4 Comments

Is it the same for the while statement?
I never use while, does not make any sense. Anyway, you could "nest" while loops the same way.
I get some odd error when i try to while ($rows AS $row) I dont think this actually works. I edited the post. The original data for this while loop was while ($row = mysql_fetch_assoc($result)) however since that was done before i need to figure out what a new statement would be.
I did not actually neaded a while loop anymore
1

Yes.., we can use foreach to print all the values in an array.

Example: I have an array called "data" (SQLITE dynamic data). I want to print all the values which are there on "data" array. By using following sample code we can print the values in a table format.

foreach ($data as $item) {
                                $date = $item['date'];
                                $url = $item['url'];
                                $name = $item['name'];
                                
                            echo"                               
                             <tr>
                                    <td>$date</td>
                                    <td>$name</td>
                                    <td>$url</td>
                                    </tr>
                            ";
                            }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.