tags:

views:

663

answers:

4

Hello i have this foreach loop that gives weird result , it displays only the first character form the db record

<?php
$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());
$data2 = mysql_fetch_array($result2) or die(Mysql_error());

foreach ($data2 as $val) {

  echo '<li><a href="media.php?id='.$val['id'].'"><img src="'.$val['fp_thumb'].'" alt="'.$val['title'].'" /></a></li>';
}
?>

and this is my DB structure

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for media
-- ----------------------------
CREATE TABLE `media` (
  `id` int(11) NOT NULL auto_increment,
  `thumb` varchar(500) NOT NULL,
  `url` varchar(500) NOT NULL,
  `fp_thumb` varchar(500) NOT NULL,
  `title` varchar(500) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `media` VALUES ('1', '22', 'http://goeshere.com', '/images/slideshow/ctmbs.jpg', 'Test 1');
INSERT INTO `media` VALUES ('2', '2', 'http://goeshere1.com', '/images/slideshow/hitlex.jpg', 'test 2');
INSERT INTO `media` VALUES ('3', '3 ', 'http://goeshere2.com', '/images/slideshow/tsord.jpg', 'test 3');

Any info would be usefull Thanks in advance and cheers ^^

A: 

You should iterate over $results with mysql_fetch_array as it only fetches one result for each call:

while ($val = mysql_fetch_array($result2, MYSQL_ASSOC)) {
  echo '<li><a href="media.php?id='.$val['id'].'"><img src="'.$val['fp_thumb'].'" alt="'.$val['title'].'" /></a></li>';
}
Zed
$data2 will be the variable containing the data from the current line, not $val => here should not use $val to do the echoing, I'd say
Pascal MARTIN
oops. the bad habit of copy-pasting.
Zed
+2  A: 

I think what you're trying to do is this:

$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());

while($data2 = mysql_fetch_array($result2)) {
    echo '<li><a href="media.php?id='.$data2['id'].'"><img src="'.$data2['fp_thumb'].'" alt="'.$data2['title'].'" /></a></li>';
}
?>

This displays all of the records in the table. Is that what you're trying to do?

The important point here is that mysql_fetch_array only retrieves a single record, and you need to call it multiple times to retrieve multiple records. It will eventually return FALSE when there are no more records to fetch.

Rob Knight
Then, he should use $data2 to do the echoing, and not $val (not initialized anywhere in your code sample)
Pascal MARTIN
Good point. Edited!
Rob Knight
ok i get this , but why for each wont work ? or how ould i have to edit it to work ? ( afcourse im going to use the while loop but , this weird thing wont let me sleep tonight )
Aviatrix
mysql_fetch_array() just returns one row at a time, not the whole query result as a big multidimensional array
Randy
+3  A: 

From the code, I would say that $data2 represents the data of 1 line from the DB.

So, it should be $data2 that contains what you want to display.

Your foreach loop is useless : if you want to display the data from 1 line, you can just use $data2 directly.


If you want to go line by line, you must not iterate over $data2, but loop while mysql_fetch_array returns data.

Something like this, I'd say :

$result2 = mysql_query("SELECT id,fp_thumb,title FROM media") or die(mysql_error());
while ($data2 = mysql_fetch_array($result2)) {
    echo '<li><a href="media.php?id='.$data2['id'].'"><img src="'.$data2['fp_thumb'].'" alt="'.$data2['title'].'" /></a></li>';
}

Also, you might want to escape the data your are outputing, to make sure you don't have any kind of HTML/javascript injection -- at least in the src and title, I'd say ; for that, you can use htmlspecialchars

Your echo would, then, probably look like this :

echo '<li><a href="media.php?id='
    . $data2['id']
    . '"><img src="'
    . htmlspecialchars($data2['fp_thumb'])
    . '" alt="'
    . htmlspecialchars($data2['title'])
    . '" /></a></li>';

(I considered id is an integer, auto-incremented or something like that, so not much of a risk)


Edit after seeing the comment on another answer

With what you first used :

$data2 is an associative array, that looks like this :

array
  'id' => int 152
  'title' => string 'this is the title' (length=17)
  'fp_thumb' => string 'http://...../' (length=13)

Which means the foreach will loop three times, and $val will be :

int 152
string 'this is the title' (length=17)
string 'http://...../' (length=13)

(One value for each iteration)

ie, $val is not an array is a scalar value.


When you do that :

$a = 152;
var_dump($a['id']);

you get

null

When doing

$a = 'this is the title';
var_dump($a['id']);

you get :

string 't' (length=1)

ie, you only get the first character when using array-access on a string, with a key that is a string.


Note that array-access on a string, with a numeric key, is used to access one character of the array :

$a = 'this is the title';
var_dump($a[3]);

gets you :

string 's' (length=1)


For more informations, see the manual page about string, especially String access and modification by character, which states :

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

And :

Non-integer types are converted to integer.

And converting 'title' to an integer gives 0 ; so, you get the first character of the string.


Hope this helps, and you understood "why" ;-)

Pascal MARTIN
Thank you very much! this explains a lot !
Aviatrix
You're welcome :-) (The first time I've had that problem with getting only one character of a string, it seemed strange to me too ^^ But there's a perfectly sane explanation ^^ ) Have fun !
Pascal MARTIN
A: 

Hmmm. I googled and found this. I'm reading and re-reading, but still not understanding why trying to access a value via a non-numeric key isn't working for me. I get the 'first letter' of the array everytime... and read that my 'temp' variable is a scalar instead of an array... But, I don't understand how to fix this. My situation: I'm gathering information out of my database (just a few values). Then, adding on one key=>value (seems to be fine). Then, when I try to access this data, no problem if I directly pull it out (for now, I have only one row). But, if I try to use foreach() or while(), I have the single letter issue. I can't understand why I should have to use mysql_fetch_array since I've already done that in my function (stored in a different document). Here's the print_r on my array, and the foreach that doesn't work, as well as the lines that do work (for a single row).

Array ( [Product] => RS-WW-103 [Size]
=> between 8' x 5' - 12' x 6' [unitCost] => 650.00 [CompleteDesc] => Remodel - Stucco | Wood Window | between 8' x 5' - 12' x 6' [qty] => 5 ) 

//doesn't work
     foreach($productInfoArray as $rowPrint){
          echo $rowPrint["qty"]."<br><br>";
          echo $rowPrint["CompleteDesc"]."<br><br>";
          echo $rowPrint["unitCost"]."<br><br>";
          } //works fine, but only one row
      $rowPrint = $productInfoArray;
          echo $rowPrint["qty"]."<br><br>";
          echo $rowPrint["CompleteDesc"]."<br><br>";
          echo $rowPrint["unitCost"]."<br><br>";

Any assistance would be MUCH appreciated!

Kirsten
use a while loop instead of a foreach :) see the answer that was accepted :)
Aviatrix