Jump to content

Help with Images


thomashw

Recommended Posts

I'm attempting to make a table with one row and 3 columns (holding three different pictures in all.) I want each picture URL to be called from my database. When I use the following code, it places the same picture across the three columns and does this three times (creating three rows.) I want a different picture to be placed across the three columns and to have only one row. What can I do?

 

Here is the code:

 

$result = @mysql_query('SELECT image FROM specials');
$num=mysql_numrows($result);
$i = 0;
while ($i < $num) {
$image=mysql_result($result,$i,"image");
?>
<table>
<tr>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
</tr>
<?
$i++;
}
echo "</table>";
?>

 

Thanks!

Link to comment
Share on other sites

Try:

 

$result = @mysql_query('SELECT image FROM specials');
$num=mysql_numrows($result);
$i = 0;
?>
<table>
<tr>
<?php
while ($i < $num) {
$image=mysql_result($result,$i,"image");
?>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<php?
$i++;
}
?>
</tr>
</table>

Link to comment
Share on other sites

I think this might work:

 

$result = @mysql_query('SELECT image FROM specials');
$num=mysql_numrows($result);
$i = 0;
?>
<table>
<tr>
<?php
while ($i < $num) {
$image=mysql_result($result,$i,"image");
?>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
</tr>
<tr>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<td>
<img src="<? echo "$image"; ?>">
</td>
<php?
$i++;
}
?>
</tr>
</table>

 

However this is not the most efficient method but it will work as long as you only want 6 images.

Link to comment
Share on other sites

What about this:

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());

echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
echo "<td><img src='{$row['image']}' /></td>";
}}
echo "</tr></table>";
?>

Link to comment
Share on other sites

Thanks, that works quite well. I'm trying to get it so when there is more than three items in the database, it will start a new row.

 

I've edited your script to look like this, but it's not working properly. It inserts ""<tr><td><img src='{$row['image']}' /></td></tr>" as the image URL and on the page.

 

$result = mysql_query('SELECT image FROM specials') or die(mysql_error());

echo "<table>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result))
$max_columns = 3;
{
echo "<tr><td><img src='{$row['image']}' /></td></tr>";
if(++$i == $max_columns){
echo "<tr><td><img src='{$row['image']}' /></td></tr>";
$i=0;
}}}
echo "</table>";
?>

 

Any ideas?

Link to comment
Share on other sites

I've modified ken2k7's code so it counts each <td> and then starts a new row after 3..

 

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());

echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
$row_amount=0;
while ($row = mysql_fetch_assoc($result)) {
$row_amount++;
if($row_amount==3){
echo "</tr><tr>";
$row_amount=0;
}

echo "<td><img src='{$row['image']}' /></td>";
}}
echo "</tr></table>";
?>

 

 

It works but it's not perfect. Like if you are only echo-ing 5 different rows then there won't be any </tr>'s ending that row. You could have it count the total amount of rows and have a separate counter besides $row_amount so when it reaches that total put how ever many empty <td></td> to fill up the space and end with a </tr> and </table>

Link to comment
Share on other sites

Uh that only works once though :(

 

Try this (I don't know if it works though)

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /></td>";
++$counter;
}}
echo "</tr></table>";
?>

Link to comment
Share on other sites

Uh that only works once though :(

 

Try this (I don't know if it works though)

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /></td>";
++$counter;
}}
echo "</tr></table>";
?>

 

that probably works better because then you don't need 2 different counters..

but mine works more than once because it resets the $row_amount after it reaches 3

Link to comment
Share on other sites

Yep, works as well. Thanks!

 

Learning PHP is difficult. :(

No it's not. You'll find your bearings real fast. It just seems hard from the methodology people use in solving problems. It's helpful to be clever at times because even the hardest thing can be looked upon as simple if you are clever. ;) Again, if you need help, feel free to ask.

 

@acidglitter: Sorry, didn't notice that. :P

Link to comment
Share on other sites

Question - to add to the above script (the latest one by Ken2k7) how could I add two rows under each image (for text)?

 

The rows would be like this:

 

IMAGE   IMAGE IMAGE
text    text    text
text    text    text

IMAGE IMAGE IMAGE
text    text    text
text    text    text

 

It's currently:

 

IMAGE IMAGE IMAGE
IMAGE IMAGE IMAGE

 

The text is held in the database the same as the image's are - just under a different field name.

 

Thanks!

Link to comment
Share on other sites

Like this?

 

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /><br />";
//  TEXT CODE HERE  //
echo "</td>";
++$counter;
}}
echo "</tr></table>";
?>

Link to comment
Share on other sites

That's actually close, but here's what I want to show up on the page:

 

<table>
<tr>
<td>IMAGE</td>
<td>IMAGE</td>
<td>IMAGE</td>
</tr>
<tr>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
</tr>
<tr>
<td>TEXT2</td>
<td>TEXT2</td>
<td>TEXT2</td>
</tr>
</table>

 

Both the texts are kept in a field called image_desc and image_desc1 in the same table as the images are.

 

I believe we'll have to change

 

$result = mysql_query('SELECT image FROM specials') or die(mysql_error());

 

to

 

$result = mysql_query('SELECT * FROM specials') or die(mysql_error());

 

so all the fields are called.

 

I'm just not sure how to do the actual script. Any help would be awesome! :)

Link to comment
Share on other sites

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /></td>";

$images = mysql_query('SELECT * FROM specials') or die(mysql_error());
if ($images || (mysql_num_rows($images) > 0)){
echo "<tr>";
while (mysql_fetch_assoc($images)){
   // what do you want here? //
}
echo "</tr>";
}

++$counter;
}}
echo "</tr></table>";
?>

Link to comment
Share on other sites

Here's what I came up with by adding to yours a little bit. It doesn't line up properly though. It lines up like this:

 

<table>
<tr>
<td>IMAGE</td>
<td>IMAGE</td>
<td>IMAGE</td>
</tr>
<tr>
<td>IMAGE</td>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
<td>IMAGE</td>
<td>IMAGE</td>
</tr>
</table>

 

Here's the code I have:

 

<?
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /></td>";

$images = mysql_query('SELECT * FROM specials') or die(mysql_error());
if ($images || (mysql_num_rows($images) > 0)){
while ($desc = mysql_fetch_assoc($images)){
if ($counter%3==0&&$counter!=0)
echo "<td>{$desc['image_desc']}</td>";
if ($counter%3==0&&$counter!=0)
echo "<td>{$desc['image_price']}</td>";
}
}

++$counter;
}}
echo "</tr></table>";
?>

 

Like mentioned before, I want the format to be like this:

 

<table>
<tr>
<td>IMAGE</td>
<td>IMAGE</td>
<td>IMAGE</td>
</tr>
<tr>
<td>TEXT</td>
<td>TEXT</td>
<td>TEXT</td>
</tr>
<tr>
<td>TEXT2</td>
<td>TEXT2</td>
<td>TEXT2</td>
</tr>
</table>

 

Thanks!

Link to comment
Share on other sites

if ($counter%3==0&&$counter!=0)
echo "<td>{$desc['image_desc']}</td>";
if ($counter%3==0&&$counter!=0)
echo "<td>{$desc['image_price']}</td>";

I'm not understanding that. :(

 

Can you explain to me what kind of text you want displayed? I mean does 1 image have 3 text fields stored in the database?

Link to comment
Share on other sites

I've got a total of four fields in the database.

 

They are:

 

image_id

image

image_desc

image_price

 

I want to display three images in a table like so:

 

<table>
<tr>
<td>image</td>
<td>image</td>
<td>image</td>
</tr>
....

 

Then I want to display the corresponding image_desc field so it displays under its image, like so:

 

<table>
<tr>
<td>image</td>
<td>image</td>
<td>image</td>
</tr>
<tr>
<td>image_desc</td>
<td>image_desc</td>
<td>image_desc</td>
</tr>

 

Then I want to display the image_price field so it displays under the above image and image_desc, like so:

 

<table>
<tr>
<td>image</td>
<td>image</td>
<td>image</td>
</tr>
<tr>
<td>image_desc</td>
<td>image_desc</td>
<td>image_desc</td>
</tr>
<tr>
<td>image_price</td>
<td>image_price</td>
<td>image_price</td>
</tr>
</table>

 

And then I want it to duplicate that, like so:

 

<table>
<tr>
<td>image</td>
<td>image</td>
<td>image</td>
</tr>
<tr>
<td>image_desc</td>
<td>image_desc</td>
<td>image_desc</td>
</tr>
<tr>
<td>image_price</td>
<td>image_price</td>
<td>image_price</td>
</tr>
<tr>
<td>image</td>
<td>image</td>
<td>image</td>
</tr>
<tr>
<td>image_desc</td>
<td>image_desc</td>
<td>image_desc</td>
</tr>
<tr>
<td>image_price</td>
<td>image_price</td>
<td>image_price</td>
</tr>
</table>

 

Here's a picture of how I want it to look:

 

images1.gif

 

 

All of the fields (image, image_desc, and image_price) are in the same table (specials.)

 

Thanks!

Link to comment
Share on other sites

Like this? (NOTE: I DIDN'T USE THE TDs. IF YOU REALLY WANT THEM, I CAN DO IT. THIS JUST SAVES A LOT OF CODING LINES)

<?php
$result = mysql_query('SELECT image FROM specials') or die(mysql_error());
$counter = 0;
echo "<table><tr>";
if ($result || (mysql_num_rows($result) > 0)){
while ($row = mysql_fetch_assoc($result)) {
if ($counter%3==0&&$counter!=0) echo "</tr><tr>";
echo "<td><img src='{$row['image']}' /><br /><{$row['image_desc']}<br />{$row['image_price']}</td>";
++$counter;
}}
echo "</tr></table>";
?>

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.