Jump to content

[SOLVED] PHP MYSQL Problem trying to solve with Conditional IF Statement ?


ofmyst

Recommended Posts

My last problem!?

 

I have a mysql database with painting info.  I use php to pull the information.  HOWEVER, I only want THREE images per line, right now it will put as many in the line as I have in the database.  I thought perhaps the best option was an IF statement. 

 

I have an ID field set up, ID =  T01, T02, T03 etc.  Is there a way to say If ID = T01 - 03 then,  if ID =  T04 - 06, etc?  Or is there a better way to structure this?

 

Here is what it currently looks like:

 

 

mysql_select_db("paintings", $con);

 

$result = mysql_query("SELECT * FROM pieces");

 

 

while($row = mysql_fetch_array($result))

 

  { 

  echo "<td align='center' width='27%'>";

  echo "<font color='white'>" . $row['Image'] . "</font>

";

  echo "

";

  echo "<font color='white'>" . $row['Link'] . "</font>

";

  echo "<font color='white'>" . $row['Title'] . "</font>

";

  echo "<font color='white'>" . $row['Medium'] . "</font>

";

  echo "<font color='white'>" . $row['Size'] . "</font>";

  echo "</td>";

  }

 

 

Thank you very much for any help you can offer.

Link to comment
Share on other sites

My last problem!?

 

I have a mysql database with painting info.   I use php to pull the information.  HOWEVER, I only want THREE images per line, right now it will put as many in the line as I have in the database.  I thought perhaps the best option was an IF statement. 

 

I have an ID field set up, ID =  T01, T02, T03 etc.  Is there a way to say If ID = T01 - 03 then,  if ID =  T04 - 06, etc?  Or is there a better way to structure this?

 

Here is what it currently looks like:

 

 

mysql_select_db("paintings", $con);

 

$result = mysql_query("SELECT * FROM pieces");

 

 

while($row = mysql_fetch_array($result))

 

  {   

  echo "<td align='center' width='27%'>";

  echo "<font color='white'>" . $row['Image'] . "</font>

";

  echo "

";

  echo "<font color='white'>" . $row['Link'] . "</font>

";

  echo "<font color='white'>" . $row['Title'] . "</font>

";

  echo "<font color='white'>" . $row['Medium'] . "</font>

";

  echo "<font color='white'>" . $row['Size'] . "</font>";

  echo "</td>";

  }

 

 

Thank you very much for any help you can offer.

You'd setup counter to do that, to do so you'd first need to initiate a variable, eg p$i = 0; This will go before your while loop. Now the rest of the code goes within your while loop. Which will be

// if $i is dividable by 3, echo a new table row
   if($i%3 == 0)
   {
       echo "</tr>\n<tr>\n";
   }

   // increment counter
   $i++;

 

Your code with admendments

echo '<table border="0" cellspacing="1" cellpadding="3">
<tr>';

// initiate counter
$i = 0;
while($row = mysql_fetch_array($result))
{
   echo '<td align="center" width="27%" style="color: white">'.
        $row['Image'] . "<br />\n".
        $row['Link'] . "<br />\n".
        $row['Title'] . "<br />\n".
        $row['Medium'] . "<br />\n".
        $row['Size'] . "</td>\n";

   // if $i is dividable by 3, echo a new table row
   if($i%3 == 0)
   {
       echo "</tr>\n<tr>\n";
   }

   // increment counter
   $i++;
}

echo '</tr>
</table>';

Link to comment
Share on other sites

DarkWater, thank you for helping.  Here is the section including the entire table.  I have the additional td's with the 8% in order to have spacers on either side of the images.  They aren't entirely necessary.

 

<table width=100%>

<tr>

<td width=8%> </td>

<?php

$con = mysql_connect("");

if (!$con)

 {

 die('Could not connect: ' . mysql_error());

 }

 

mysql_select_db("paintings", $con);

 

$result = mysql_query("SELECT * FROM pieces");

 

 

while($row = mysql_fetch_array($result))

 

$id=ID("D");

if ($id=="TD01,TD02,TD03")

 {

 echo "<td align='center' width='27%'>";

 echo "<font color='white'>" . $row['Image'] . "</font><br>";

 echo "<br>";

 echo "<font color='white'>" . $row['Link'] . "</font><br>";

 echo "<font color='white'>" . $row['Title'] . "</font><br>";

 echo "<font color='white'>" . $row['Medium'] . "</font><br>";

 echo "<font color='white'>" . $row['Size'] . "</font>";

 echo "</td>";

 }

mysql_close($con);

?>

 

<td width=8%> </td>

 

</tr>

 

</table>

Link to comment
Share on other sites

DarkWater/WildTeen-

 

Thank you!  You have me on the right track.  It is working except that the first line is one image - then it does three to a line.  I have something wrong.  Here is what I have so far:

 

<table width=100%>

<tr>

<?php

$con = mysql_connect("");

if (!$con)

  {

die('Could not connect: ' . mysql_error());

}

 

mysql_select_db("paintings", $con);

 

$result = mysql_query("SELECT * FROM pieces");

 

// initiate counter

 

$i = 0;

 

 

while($row = mysql_fetch_array($result))

{

    echo '<td align="center"  style="color: white">'.

        $row['Image'] . "<br />\n".

        $row['Link'] . "<br />\n".

        $row['Title'] . "<br />\n".

        $row['Medium'] . "<br />\n".

        $row['Size'] . "</td>\n";

 

    // if $i is dividable by 3, echo a new table row

    if($i%3 == 0)

    {

        echo "</tr>\n <tr>\n";

  }

 

    // increment counter

  $i++;

}

 

echo '</tr>

</table>';

 

 

to see what it looks like, if you want:  www.sharondross.com/images/tinker/tinker5.php 

 

Also, I would like to have a space between image row and link line.  And then a space between the rows so they don't run together. 

 

Thank you.

Link to comment
Share on other sites

I thought I could take your solution and figure on my own how to format it the way I needed it.  Essentially I have a table with the first row four columns (the first three with info in them) and the fourth showing the first two paintings one on top of the other.  Then start another row that spanned the four columns and showed the next three paintings, then new row next three, etc.  I works somewhat but adds in an extra </tr> just before the second row starts (which I found by doing a view source).  I cannot seem to find where my mistake is.  I left the info in the first few columns out to keep it cleaner. 

 

<table width=100%>

<tr>

<td width=10%> </td>

<td width=39%> </td>

<td width=10%> </td>

<td width=40%>

<table>

<tr>

 

<?php

$con = mysql_connect("");

if (!$con)

  {

die('Could not connect: ' . mysql_error());

}

 

mysql_select_db("paintings", $con);

 

$result = mysql_query("SELECT * FROM pieces");

 

// initiate counter

 

$i = 2;

 

 

while($row = mysql_fetch_array($result))

{

    echo '<td align="center"  style="color: white">'.

        $row['Image'] . "<br><br />\n".

        $row['Link'] . "<br />\n".

        $row['Title'] . "<br />\n".

        $row['Medium'] . "<br />\n".

        $row['Size'] . "<br><br><br><br></td>\n";

 

// if $i is equal to , echo a new table row

    if($i == 2 )

    {

        echo "</tr>\n <tr>\n";

  }

 

// if $i is equal less than 3, echo a new table row then end 2 ptg table

    if($i == 3 )

    {

        echo "</tr>\n </table>\n </td>\n </tr>\n <tr>\n <td colspan=4>\n <table>\n <tr>\n";

  }

 

    // if $i is dividable by 3, echo a new table row

    if($i%3 == 0)

    {

        echo "</tr>\n <tr>\n";

  }

 

    // increment counter

  $i++;

}

 

echo '</tr>

</table>';

 

mysql_close($con);

?>

</td></tr></table>

 

You can see what I came up with here -- >  http://www.sharondross.com/images/tinker/test2.php

 

I'm sorry to be such a bother.

 

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.