0

I have a php code like this:

$categories_query = tep_db_query("select categories_id, categories_name from categories order by categories_name");
while ($categories = mysql_fetch_array($categories_query)) {
$categories_array[] = array('id' => $categories['categories_id'], 'text' => $categories['categories_name']);
}

question is how can I replace the while loop with for example foreach so I can first fetch mysql array, then I want to edit some values and then pass it on to a loop? I tried different versions of loops but they don't give me the same result as the while loop does.

3
  • Anything wrong with while? Commented Nov 23, 2014 at 20:05
  • 1
    Don't use [mysql] functions anymore. They are deprecated, move on to mysqli or pdo Commented Nov 23, 2014 at 20:09
  • The tep_db_query makes me think this is oscommerce, doubt he can easily change Commented Nov 23, 2014 at 20:25

3 Answers 3

1

PHP doesn't have a mysql_fetch function that delivers all rows at once which could be used in a foreach look.

Try to get all rows in one loop and build an array with all rows. Then, iterate over this array and perform operations as you described.

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

3 Comments

well, putting the result-rows in an array is what he does right now.
ok, but there should be a second loop to perform the desired operations.
rhanks that explained why I was failing so far
0

what you have tried looks good. there is no "fetch all" function in the old and deprecated mysql library. you should switch to mysqli or PDO instead.
in PDO you can just grab all the result-data with $statement->fetchAll() for example.

if you still want to solve your problem with the old mysql library, then:

$categories_query = tep_db_query("select categories_id, categories_name from categories order by categories_name");
while ($categories = mysql_fetch_array($categories_query)) 
{
    $categories_array[] = array('id' => $categories['categories_id'], 'text' =>     $categories['categories_name']);
}

// do something with your $categories_array here

foreach($categories_array AS $array => $row)
{
    // you can output / access each row here e.g. with: $row['id']
    // or you can do a second foreach-loop for the columns:
    foreach($row AS $col => $data)
    {
        echo $data;
    }
}

1 Comment

In the first foreach loop I just put "categories_array2[] = $row;" so the end result is exactly the same as the while loop, but now I can preform my actions before executing the for loop, thanks!
0

From the looks of it the purpose of your loop is just to rename some table columns.

Just do:

<?php
  $categories_array = mysqli_query( mysqli_fetch_assoc($con,"SELECT categories_id AS id, categories_name AS text FROM categories ORDER BY categories_name") );
?>

1 Comment

my problem is a little more complicated. this is what the code looked like before but the problem is that this whole code is inside another loop. so im trying to get rid of database query in loop and replace it with an array to make it faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.