Jump to content

foreach loops


dsartain

Recommended Posts

Is this the correct syntax??  I've never done one of these before....I'm trying to dynamically display images...work on making it pretty later...

 

$dbconn=mysql_connect($hostname, $user, $password);
mysql_select_db($dbname, $dbconn);

$sql="SELECT * FROM `thumbnail_table`";
$runquery=mysql_query($sql);
$result=mysql_fetch_array($runquery);
echo mysql_error();

foreach($result)
{
'<img src="'.$result['pic_link'].'" /> <br />';
}

Link to comment
https://forums.phpfreaks.com/topic/37858-foreach-loops/
Share on other sites

Yeah, I saw that just before you mentioned it...I put the echo tag in there....but all I get are red X boxes....

 

To test it I did:

foreach($result as $k=>$v)
{
echo $v['pic_link']."<br />";
//  echo '<img src="'.$v['pic_link'].'" /> '; //This line isn't valid code either.
}

 

but the pic_link field in the table gives me a full URL for each one....what am I missing??

 

The output was...

1

1

1

1

h

h

 

 

Entire code as is now:


<?php

//vars for $dbconn omitted
$dbconn=mysql_connect($hostname, $user, $password);
mysql_select_db($dbname, $dbconn);
echo mysql_error();
if (!$dbconn)
{
echo "Could Not Connect to DB!";
}




$sql="SELECT * FROM `thumbnail_table`";
$runquery=mysql_query($sql);
$result=mysql_fetch_array($runquery);


foreach($result as $k=>$v)
{
echo $v['pic_link']."<br />";
//  echo '<img src="'.$v['pic_link'].'" /> '; //This line isn't valid code either.
}

/*
foreach($result as $value)
{
'<img src="'.$result['pic_link'].'" /> <br />';
}
*/
?>

Link to comment
https://forums.phpfreaks.com/topic/37858-foreach-loops/#findComment-181218
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.