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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.