Showing pictures using links in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Showing pictures using links in database

Post by NiGHTFiRE »

Hey,
I've got a MySQL table with a column where diffrent pictures names are in.
This is my code:

Code: Select all

<?php

  include("db.php");
  include("functions.php");

  $sql = "SELECT COUNT(id) FROM auction where status='ny'";
  $result = mysql_query($sql);
  echo mysql_error();
  //$limit = 3; // Antal "nya" nyheter

  $row = @mysql_fetch_array($result); 
  $numrows = mysql_result($result, 0); // antal i databasen 

  if (mysql_result ($result , 0) <= 0)
  {
      print '<br>Finns inga auktioner ännu!<br>';
  }

  else
  {
      $result = mysql_query("SELECT * FROM auction where status='ny' ORDER BY id DESC LIMIT 3");

      for ($x = 0; $x < mysql_num_rows($result); $x++)
      {
          $row = mysql_fetch_array($result);

          print '<table align="center" width="270" border="0"><tr><td class="svart_ram">';

          print '<table border="0" width="220"><tr><td>'.$row['datum_borja'].'<br><b>'.$row['auktionnamn'].'&nbsp;</b></td>';
          //print '<td>Av: '.$row['skrivar_id'].'</td>';
          print '</tr></table><hr noshade="noshade">';
		  $sms = "För att lägga ett bud på denna auktion smsa ". $row['item'];
		  $sms .= " till 72550";
		  echo $sms;
		  echo "<br>";
		  $var .= "<img title=\"Produktnamn\"";
$var .= "src=\"img/products/". $row['bild']."\"";
$var .= " border=\"0\" />";
echo $var;
echo "<br>";
          $text = $row['info'];
		  echo $text;
		  echo "<br>";
          //print text_replace($text);

          print '</tr></td></table><br>';

      }
  }

?>
The problem is that at the first box it shows 1 picture. At the second box it shows two pictures. The picture in box one and the picture that should be displayed in box 2. And in box three there's 3 pictures being displayed. The picture in box one and the picture in box two. And the picture that should be displayed in box 3.
How would I solve that the correct picture shows at the correct box?
Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I read your problem about 5 times and could not understand what you meant.. do you have a live example I can look at or could you please explain as clear as possible the problem?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

http://wmegn.mine.nu/emil/auctions.php

Sorry that i've explained porly
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Try this: (untested)

Code: Select all

<?php 

  include("db.php"); 
  include("functions.php"); 

  $sql = "SELECT COUNT(id) FROM auction where status='ny'"; 
  $result = mysql_query($sql); 
  echo mysql_error(); 
  //$limit = 3; // Antal "nya" nyheter 

  $row = @mysql_fetch_array($result); 
  $numrows = mysql_result($result, 0); // antal i databasen 

  if (mysql_result ($result , 0) <= 0) 
  { 
      print '<br>Finns inga auktioner ännu!<br>'; 
  } 

  else 
  { 
      $result = mysql_query("SELECT * FROM auction where status='ny' ORDER BY id DESC LIMIT 3"); 

      for ($x = 0; $x < mysql_num_rows($result); $x++) 
      { 
          $row = mysql_fetch_assoc($result); // mysql_fetch_assoc instead of mysql_fetch_array

          print '<table align="center" width="270" border="0"><tr><td class="svart_ram">'; 

          print '<table border="0" width="220"><tr><td>'.$row['datum_borja'].'<br><b>'.$row['auktionnamn'].'&nbsp;</b></td>'; 
          //print '<td>Av: '.$row['skrivar_id'].'</td>'; 
          print '</tr></table><hr noshade="noshade">'; 
                  $sms = "För att lägga ett bud på denna auktion smsa ". $row['item']; 
                  $sms .= " till 72550"; 
                  echo $sms; 
                  echo "<br>"; 
                  $var .= "<img title=\"Produktnamn\""; 
$var .= "src=\"img/products/". $row['bild']."\""; 
$var .= " border=\"0\" />"; 
echo $var; 
echo "<br>"; 
          $text = $row['info']; 
                  echo $text; 
                  echo "<br>"; 
          //print text_replace($text); 

          print '</tr></td></table><br>'; 

      } 
  } 

?>
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

I still get the same result though.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

This WILL work (even thought its untested... :lol: )

Code: Select all

<?php 

  include("db.php"); 
  include("functions.php"); 

  $sql = "SELECT COUNT(id) FROM auction where status='ny'"; 
  $result = mysql_query($sql); 
  echo mysql_error(); 
  //$limit = 3; // Antal "nya" nyheter 

  $row = @mysql_fetch_array($result); 
  $numrows = mysql_result($result, 0); // antal i databasen 

  if (mysql_result ($result , 0) <= 0) 
  { 
      print '<br>Finns inga auktioner ännu!<br>'; 
  } 

  else 
  { 
      $result = mysql_query("SELECT * FROM auction where status='ny' ORDER BY id DESC LIMIT 3"); 

      for ($x = 0; $x < mysql_num_rows($result); $x++) 
      { 
          $var = '';
          $row = mysql_fetch_assoc($result); // mysql_fetch_assoc instead of mysql_fetch_array 

          print '<table align="center" width="270" border="0"><tr><td class="svart_ram">'; 

          print '<table border="0" width="220"><tr><td>'.$row['datum_borja'].'<br><b>'.$row['auktionnamn'].'&nbsp;</b></td>'; 
          //print '<td>Av: '.$row['skrivar_id'].'</td>'; 
          print '</tr></table><hr noshade="noshade">'; 
                  $sms = "För att lägga ett bud på denna auktion smsa ". $row['item']; 
                  $sms .= " till 72550"; 
                  echo $sms; 
                  echo "<br>"; 
                  $var .= "<img title=\"Produktnamn\""; 
$var .= "src=\"img/products/". $row['bild']."\""; 
$var .= " border=\"0\" />"; 
echo $var; 
echo "<br>"; 
          $text = $row['info']; 
                  echo $text; 
                  echo "<br>"; 
          //print text_replace($text); 

          print '</tr></td></table><br>'; 

      } 
  } 

?>
You never cleared out the $var you just kept adding more and more HTML to it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You need to clear $row. The var is somehow being added to itself rather than writing over it.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Everah wrote:You need to clear $row. The var is somehow being added to itself rather than writing over it.
It think its $var more than $row
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try this...

Code: Select all

<?php 
include 'db.php';
include'functions.php';

if (!$result = mysql_query('SELECT * FROM auction where status=\'ny\' ORDER BY id DESC LIMIT 3'))
{
	die('Could not grab the data: ' . mysql_error());
}

if (mysql_num_rows($result))
{
	echo '<table align="center" width="270" border="0"><tr><td class="svart_ram">';
	while ($row = mysql_fetch_array($result))
	{
		echo '<table border="0" width="220"><tr><td>' . $row['datum_borja'] . '<br><b>' . $row['auktionnamn'] . '&nbsp;</b></td>';
		echo '</tr></table><hr noshade="noshade">'; 
		echo 'För att lägga ett bud på denna auktion smsa ' . $row['item'] . ' till 72550';
		echo '<br>';
		echo '<img title="Produktnamn" src="img/products/'. $row['bild']. '" border="0" />';
		echo '<br>';
		echo $row['info'];
		echo '<br>'; 
	}
	echo '</tr></td></table><br>';
}
else
{
	echo '<br>Finns inga auktioner ännu!<br>';
}
?>
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

tecktalkcm0391 wrote:This WILL work (even thought its untested... :lol: )

Code: Select all

<?php 

  include("db.php"); 
  include("functions.php"); 

  $sql = "SELECT COUNT(id) FROM auction where status='ny'"; 
  $result = mysql_query($sql); 
  echo mysql_error(); 
  //$limit = 3; // Antal "nya" nyheter 

  $row = @mysql_fetch_array($result); 
  $numrows = mysql_result($result, 0); // antal i databasen 

  if (mysql_result ($result , 0) <= 0) 
  { 
      print '<br>Finns inga auktioner ännu!<br>'; 
  } 

  else 
  { 
      $result = mysql_query("SELECT * FROM auction where status='ny' ORDER BY id DESC LIMIT 3"); 

      for ($x = 0; $x < mysql_num_rows($result); $x++) 
      { 
          $var = '';
          $row = mysql_fetch_assoc($result); // mysql_fetch_assoc instead of mysql_fetch_array 

          print '<table align="center" width="270" border="0"><tr><td class="svart_ram">'; 

          print '<table border="0" width="220"><tr><td>'.$row['datum_borja'].'<br><b>'.$row['auktionnamn'].'&nbsp;</b></td>'; 
          //print '<td>Av: '.$row['skrivar_id'].'</td>'; 
          print '</tr></table><hr noshade="noshade">'; 
                  $sms = "För att lägga ett bud på denna auktion smsa ". $row['item']; 
                  $sms .= " till 72550"; 
                  echo $sms; 
                  echo "<br>"; 
                  $var .= "<img title="Produktnamn""; 
$var .= "src="img/products/". $row['bild']."""; 
$var .= " border="0" />"; 
echo $var; 
echo "<br>"; 
          $text = $row['info']; 
                  echo $text; 
                  echo "<br>"; 
          //print text_replace($text); 

          print '</tr></td></table><br>'; 

      } 
  } 

?>
You never cleared out the $var you just kept adding more and more HTML to it.
Thanks, works good now :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Did you try the other code I posted. I want to know if it works.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

No I haven't tryed yours.
And if I do I gotta change some stuff so the right id of the auction and everything works.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You shouldn'y have to change it any more than you would have to change what you posted. I used all of your parameters and queries.

No biggie. If you decide to try it and it flies, let me know. Thanks.
Post Reply