Page 1 of 1

PHP & MySQL formatting problem

Posted: Mon Aug 16, 2004 5:26 pm
by irfreh
Hello there,

I dont know if i am posting in the correct section of this forum because i am new here and in a bit of a rush to solve my problem. So if i am in the wrong section feel free to curse at me all you want, but just please help me with my problem.

The problem that i am having is trying format the display of a page that is pulling information from a MySQL database. I am creating a online photo album and i require the pictures to be displayed across the screen in rows of three instead of directly under one another (because of rows created from using html tables).

Is there any suggestions on different format options in HTML and PHP, any links to online sources would be of great help.

If my problem has not been explained well please let me know also, so that i can try explaining it more better.

Thankyou to anyone taking the time to read this.

I Reh

Posted: Mon Aug 16, 2004 5:39 pm
by tim
seems like this is not a php issue

an html issue. read up on creating tables

<tr> and <td>, consider <tr> as the columns, <td> as the seats in the columns.

Mess around, make a test table, you can do this w/o even being online.

Code: Select all

<table><tr><td>hi</td><td>hi</td></tr>
<tr><td>hi</tr></td></table>
mess around with that, add pieces to it, remove pieces to it. see what does what

let us know if u have any other issues tieing it into your SQL.

Posted: Tue Aug 17, 2004 10:35 am
by reh
Hello there Tim, or anybody else reading this post,

Its me irfreh, this forum was not letting me login with my regular username so i had to register again and login under another identitiy. Proper secret spy stuff huh.

Anyway i have tried to mess around with the table tags but i think that my problem needs to be tackled via a different route. The reason being is that as you already know, when using a table to display information pulled from a database the rows are created depending on the amount of information being pulled from the database. So when my table is creating rows it is (as it is supposed to) posting them directly under one another. This would be fine if i was displaying text information. But because i am creating an online portfolio i need images to be displayed in three's on each row.

As used in the getty images website:
link
(i gave the full link so that you dont have to mess with the search)

the code i used for my tables (just incase it is a HTML problem) is posted below.

Code: Select all

echo (\"<table>
<tr><td><img src ='pics/$img_name'></td></tr>
</table>\");
         }
Thankyou again to whoever takes the time to read this post.

I Reh

Posted: Tue Aug 17, 2004 4:50 pm
by tim
Could you post the code you use for your SQL side of things?

the table code should be placed inside a while loop, or some sort of loop. you should also be using mysql_fetch_array or something similar.

post the rest of your code.

Posted: Wed Aug 18, 2004 3:48 am
by reh
Hello there Tim,

I am posting the code for the dispaly page only, but if you require the code for the other pages as well please let me know. I have taken out the password and username just incase of some shady forum surfers using the details for mischievous use.

Code: Select all

<? 
    $usr = "****"; 
    $pwd = "*****"; 
    $db = "******"; 
    $host = "localhost"; 

    # connect to database 
    $cid = mysql_connect($host,$usr,$pwd); 
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    } 

?> 
<HTML> 
<HEAD> 
  <TITLE>Test</TITLE>
</HEAD> 


<? 
 

    # setup SQL statement 
    $SQL = " SELECT * FROM portfolio "; 
    $SQL = $SQL . 

    # execute SQL statement 
    $retid = mysql_db_query($db, $SQL, $cid); 

    # check for errors 
    if (!$retid) { echo( mysql_error()); } 
    else { 

        # display results 
        echo ("<P><DT><B>$category</B><BR>\n\n"); 
        while ($row = mysql_fetch_array($retid)) { 
            $pic_ref = $row["pic_ref"]; 
            $pic_name = $row["pic_name"];
$img_name = $row["img_name"];
$pic_desc = $row["pic_desc"];
$sub_date = $row["sub_date"];
$photographer = $row["photographer"];


            echo ("<table>
</tr><td><img src ='pics/$img_name'>&nbsp;$pic_name</td></tr>
</table>");
         } 
        echo ("</DT></P>"); 
    } 
?> 

</BODY> 
</HTML>
Thanks again,
I Reh

Posted: Wed Aug 18, 2004 4:07 am
by feyd
looks like you are outputting a new table for each row returned from the query, why not make the whole thing a larger table, and stack every 3 or whatever images in <td>'s then a new <tr> if needed..

then, using the number of records found add (one less than the total images per table row you want) and use the modulo to retrieve the remainder of dividing by the total number of images per table you want.. this will give you the number to use in a colspan for the filling in the last row (if needed).. if the number is zero, you're done..

Posted: Wed Aug 18, 2004 5:39 am
by reh
Thanks feyd,

I was thinking that i needed to put put the images in <TD> tags depending on how many i needed.(in my case three), But what i am having trouble trying to get my head around is say that i used a bigger table eg.

Code: Select all

echo ("<table> 
<tr><td><img src ='pics/$img_name'></td><td><img src ='pics/$img_name'></td><td><img src ='pics/$img_name'></td></tr> 
</table>");
How will the different images being pulled from the database be posted in the relevant <td> columns. Because what i have done above will just display the same images in the <td> columns.

(apologies for the newbie question replies)

Thanks again
I Reh

Posted: Wed Aug 18, 2004 11:10 am
by feyd
JAM | changed to code instead of php tags.
feyd | changed back to php tags now that they are working again.

Code: Select all

<?php
$usr = "****"
$pwd = "*****";
$db = "******";
$host = "localhost";

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

$output = '<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>';

# setup SQL statement
$SQL = " SELECT * FROM portfolio ";
$SQL = $SQL .

# execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid) or die(mysql_error());

# display results
$output .= "<P><DT><B>$category</B><BR>\n\n";
$output .= "<table>\n\n";
$howmany = mysql_num_rows($retid);
$rowmax = 3;
for($x = 0; $row = mysql_fetch_array($retid); $x++)
{
if($x % $rowmax == 0)
$output .= "<tr>\n";
$pic_ref = $row["pic_ref"];
$pic_name = $row["pic_name"];
$img_name = $row["img_name"];
$pic_desc = $row["pic_desc"];
$sub_date = $row["sub_date"];
$photographer = $row["photographer"];

$output .= "<td><img src =\"pics/$img_name\">$pic_name</td>";
if($x % $rowmax == $rowmax - 1)
$output .= "\r</tr>\n\n";
}

if($left = (($howmany + $rowmax - 1) % $rowmax))
$output .= '<td colspan="' . $left . '">&nbsp' . ";</td>\n</tr>\n\n";

$output .= "</table>\n\n";

$output .= "</DT></P>";

$output .= '</BODY>
</HTML>';

echo $output;
?>

Posted: Thu Aug 19, 2004 8:34 am
by reh
Thank you soooo much for doin the code for us, just trying to understand it now, but it works and it is exactly what i needed.

thanks again

I Reh

Re: [SOLVED] PHP & MySQL formatting problem

Posted: Tue Sep 16, 2008 2:18 pm
by Weasel5-12
wow, very very similar to a situation im in myself !

cheers bro !! this is great (although i dont fully understand how it all works... yet) :D

Re: [SOLVED] PHP & MySQL formatting problem

Posted: Wed Nov 11, 2009 5:44 pm
by starram
Hello feyd

I have choosed following line from the code you wrote.

I am not able to understand how this line will behave?

" for($x = 0; $row = mysql_fetch_array($retid); $x++) "

Or is it incorrect?

Please update.
Regards
Ram

Re: [SOLVED] PHP & MySQL formatting problem

Posted: Thu Dec 02, 2010 12:25 am
by imadbaloch
<?php $sql="select id from table_name where id=$_GET['id']";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
?><tr><td><img scr="images/<?php echo $row['image']?>"></td></tr><?php } ?>