Sms
Sms

Reputation: 139

PHP: using foreach to display specific data in a multidimensional array

I've got an array containing data queried from mysql table and i would like to display specific data (specific column data) in the array using foreach.

+------+-------+------+-----+
| id   | name  | sex  | age |
+------+-------+------+-----+
|    1 | tom   | m    |  18 |
|    2 | jane  | f    |  32 |
|    4 | Doyle | m    |  25 |
+------+-------+------+-----+

Assuming array name is $candidates, What i need to display from $candidates are just the names from the colum 'name'....

I have done this perfectly using while loop but I dont want to run a database query again since the data already exist in the array $candidates

Here are the the codes:

1. Class function performing the query

       <?php

    class pubCourses {
            function getPubRegCand($org_id, $c_id){
            //fetch the list of registered candidates for this course
            $query_reg_cand = @mysql_query("select * from public_reg_cand where org_id='".$org_id."'");
            $cand = array();
            $cand = @mysql_fetch_assoc($query_reg_cand);

            return $cand;
                    }
          }
        ?>

2. Calling the class function 

    <?php
        $obj = new pubCourses();
        $candidates = array();
        if(isset($_GET['c_id']) && isset($_GET['org_id'])){
            $candidates = $obj->getPubRegCand($_GET['org_id'], $_GET['c_id']);
        }
    ?>

3. Foreach statement to display the candidates names


      <?php

        foreach($candidates as $id=>$cands){
      ?>
     <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td class="bold">Participant&#039;s Name: <?php echo ucwords($cands['name']); ?></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
     </tr>
     <?php
        }
     ?>

The foreach statement above is not displaying the 'names' i need rather it's displaying the first character of the first row i.e 1, t, m, 1...

Kindly help me out with this...Thanks

Upvotes: 0

Views: 1795

Answers (2)

bipen
bipen

Reputation: 36531

since you are returning a row from the db...

use

 while($row=mysql_fetch_array($query_reg_cand)) {
   $cand[] = $row;
 }

instead of

$cand = @mysql_fetch_assoc($query_reg_cand);

this should do.... no need to use $id=>$cands (unless u want key). u can just do..

 foreach($candidates as $cands){...

and lastly

Use of mysql extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used

Upvotes: 0

Morgan Wilde
Morgan Wilde

Reputation: 17295

Why not do a regular while loop with your query?

$mysqli = new mysqli("localhost", "user", "password", "db_name");
$query = "SELECT name FROM your_table";

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        echo <<<HTML 
<tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td class="bold">Participant&#039;s Name: {$row['name']}</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
HTML;
        //echo $row['name'];
    }
}

Upvotes: 1

Related Questions