Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to read array by row,,?i have tried everything..

Array:
Array ( [0] => Array ( [category_id] => 2 [title] => Deck Courses [course_title] => Adaptive Steering ) [1] => Array ( [category_id] => 2 [title] => Deck Courses [course_title] => Test subject 2 ) [2] => Array ( [category_id] => 2 [title] => Deck Courses [course_title] => Test subject 4 ) [3] => Array ( [category_id] => 3 [title] => Engine Room Courses [course_title] => Test subject 3 ) )


Error: mysql_fetch_assoc() expects parameter 1 to be resource, array given


PHP
<?php

        //print_r($categories);exit;
        $current_cat = null;

        //while ($row = $categories->fetch())
        //while ($row = mysqli_fetch_row($categories))
        //while($row = mysql_fetch_array($categories))
        //while($row = mysql_fetch_row($categories))
        while($row = mysql_fetch_assoc($categories))
        {
        //print_r($row);exit;
        if ($row["title"] != $current_cat)
        {
        $current_cat = $row["title"];
          echo $current_cat; // course category name
          echo $row["course_title"]; // course name
        }
        else
        {
          echo $row["course_title"]; // course name 
        }
        } ?>
Posted
Updated 22-Sep-13 20:38pm
v2

1 solution

Got solution,,,it worked


PHP
<?php

      
       $current_cat = null;

       foreach ($categories as $row)
       {
       if ($row["title"] != $current_cat)
        {
          $current_cat = $row["title"];
        
          echo "<div> $current_cat </div>"; 
          echo $row["course_title"]; 
        }
        else
        {
          echo $row["course_title"]; 
        }
       }
        ?>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900