Bhaskar Wankhede
Bhaskar Wankhede

Reputation: 69

PHP echo array value after while loop

How can I echo out value after the while loop. If I do the echo in below code its says Undefined index.

$sql_cast = "SELECT *
            FROM
              title_cast
              INNER JOIN title ON (title_cast.id_title = title.id)
              INNER JOIN `cast` ON (title_cast.id_cast = `cast`.id)
            WHERE
              title_cast.id_title = '1'";
$result_cast = mysql_query($sql_cast) or die('log error with' .mysql_error());
$cast = array();
while ($row = mysql_fetch_assoc($result_cast)) {
                $id = $row['id'];
                $name = $row['name'];
                $img = $row['photo_localurl'];
                $poster = str_replace("./", "lib/", $img);

                $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
                echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";

Upvotes: 0

Views: 12019

Answers (6)

Amna Ahmed
Amna Ahmed

Reputation: 1964

There are two problems with your code, First is that $cast[] is a two-dimensional array so it contains arrays at each of its index, so you must use

$cast[$i]['id'] 

Where i will be the counter variable that will iterate through all the indexes. Secondly change

$cast['poster'] 

to

$cast['img']

Hope this helps.

Upvotes: 0

Sahin Yanlık
Sahin Yanlık

Reputation: 1201

print_r($cast[0]); or you can use $cast[0]['id'] ;

Upvotes: 0

Mas User
Mas User

Reputation: 166

Try this:

<?php
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
}

foreach($cast as $rows) {
    echo $rows['id']." ".$rows['name']." ".$rows['img']."<br />";
}
?>

Upvotes: 0

ThomasM
ThomasM

Reputation: 2677

That's because you are pushing a new array in $cast at each index..

So you should echo like this..

$i = 0;
while ($row = mysql_fetch_assoc($result_cast)) {
      $id = $row['id'];
      $name = $row['name'];
      $img = $row['photo_localurl'];
      $poster = str_replace("./", "lib/", $img);

      $cast[] = array('id' => $id, 'name' => $name, 'img' => $poster);
      //$cast[] = $row;

      //var_dump($cast);
      echo $cast[$i]['id'] . " " . $cast[$i]['name'] . " " . $cast[$i]['poster']."<br />";
      $i++;
}

Upvotes: 0

Franquis
Franquis

Reputation: 743

Maybe you should do:

$cast = array('id' => $id, 'name' => $name, 'img' => $poster);
                //$cast[] = $row;
                }
                //var_dump($cast);
echo $cast['id'] . " " . $cast['name'] . " " . $cast['poster']."<br />";

because if you use $cast[], it will append the new array to your array..

Upvotes: 0

Oldskool
Oldskool

Reputation: 34837

Within the while loop, you set the cast array content using $cast[] syntax. This will create a numerical index, starting at 0, then 1 and so on, so you're creating an array that looks like this:

$cast = array(
    0 => array('id' => $id, 'name' => $name, 'img' => $poster),
    1 => array('id' => $id, 'name' => $name, 'img' => $poster)
);

You need to include the numerical key of the array that you want to echo. For example, if you want to echo the first row:

echo $cast[0]['id']; // Echo the id of the first result

If you want to echo ALL of the rows, use foreach:

foreach($cast as $row) {
    echo $row['id'];
}

Upvotes: 2

Related Questions