Page 1 of 1

having pics list horizantally rather then vertically

Posted: Mon Aug 12, 2002 11:12 pm
by Rawhead
Hi all! I must first note that I know absolutly NOTHING about how php gets around, I's almost a total mystery to me. Having said that I asked a guy to write code for food one day, The point of this script is to list all of the pictures associated with a field. It displays horizantally, were as I get 5 images up and down...this sucks :( I need the images to go from left to right, preferably in 2 rows (3 on top and 2 on bottom) Now for my question
WHERE (@#$%) is this being specified in this code??? (And now to annoy the moderator) how do I fix it??

<?

//don't run query unless id>0 This is a fix to an earlier problem I had

if($iID>0)

{

$sWhere =" WHERE id='$iID'";

$sSQL = "SELECT * FROM boat_list $sWhere ";

$rsResult = mysql_query($sSQL) or die("Query failed");
if ($rsLine = mysql_fetch_array($rsResult, MYSQL_ASSOC)) {

$iOwnerID = $rsLine["boat_owner_contact_id"];
$sSQL = "SELECT filename FROM boat_picture_matrix WHERE boat_list_id='".$rsLine["id"]."'";
$rsImages = mysql_query($sSQL) or die("Query failed");
while($rsImage=mysql_fetch_array($rsImages, MYSQL_ASSOC)){
$sImage.="<a href=\"images/upload/".$rsImage["filename"]."_large.jpg\" target=_blank><img src=\"images/upload/".$rsImage["filename"].".jpg\"></a><br>";

}
mysql_free_result($rsImages);

?>

okay so $rsImage Is the var that controlls this so it's safe to assume that the line $sSQL = $rsImage (some of you are saying "no s#$% Sherlock) boat_picture_matrix is the table the images draw from, I still can't see where placement params might go.
Thanks!

Posted: Tue Aug 13, 2002 12:16 am
by Hebbs
I use something like the following to preformat a search result page with a pre-dertermined number of columns ($numfields).

I havent tested this as it's written here, just modified it a bit to reflect what I think you want.

It should give you the idea though.

BTW just in case you miss it, the following will need to be in a while loop.

Code: Select all

<DIV align="center"><CENTER>
	<TABLE border="0" cellpadding="$numfields" cellspacing="2">
		<TR><TD colspan='3'>Title if required<br></font></TD></TR>
		<?php
		echo "</tr>\n";
		// Traverse along row for number of fields required.
		for ($k = 0; $k < $numfields); $k++) &#123;
			if ($k == 0) &#123;
			printf (" <td nowrap><ahref="images/upload/".$rsImage&#1111;"filename"]."_large.jpg" target=_blank><img src="images/upload/".$rsImage&#1111;"filename"].".jpg"></a><br>"; 

</td> ");
			&#125;else&#123;
			printf (" <td nowrap>&nbsp; %s &nbsp;</td>\n ", htmlspecialchars ($row&#1111;$k]));
			&#125;
		&#125;
		echo "</TR>";
		?>
	</TABLE>
</CENTER></DIV>
Hebbs

Posted: Tue Aug 13, 2002 9:06 am
by lc
Actually.. if you want your images to be listed next to each other I'd say it's a safe bet to say that can be done by removing the <br> (newline) code and placing a nice space there.

SO this line not:
src=\"images/upload/".$rsImage["filename"].".jpg\"></a><br>";

BUT:
src=\"images/upload/".$rsImage["filename"].".jpg\"></a>&nbsp";

I dunno mysql but I'd suspect that this does it.

Posted: Tue Aug 13, 2002 9:16 am
by llimllib
LC's on the right tack. Change this:

Code: Select all

while($rsImage=mysql_fetch_array($rsImages, MYSQL_ASSOC))&#123;
$sImage.="<a href="images/upload/".$rsImage&#1111;"filename"]."_large.jpg" target=_blank><img src="images/upload/".$rsImage&#1111;"filename"].".jpg"></a><br>";
&#125;
to this:

Code: Select all

$i = 1;
while($rsImage=mysql_fetch_array($rsImages, MYSQL_ASSOC))&#123;
$sImage.="<a href="images/upload/".$rsImage&#1111;"filename"]."_large.jpg" target=_blank><img src="images/upload/".$rsImage&#1111;"filename"].".jpg"></a>&nbsp;";
if($i % 3 == 0)
    $sImage .= '<br>';
$i++;
$sImage = '';
&#125;
I haven't tested it, but it should work.

PS. It's ok to ask 'how do i fix this two-line snippet' - it's the 'I NEED a script tomorrow that emulates Hotmail' that we get all twisted over :)

<edit>Edited for correctness. the line in the if() statement was wrong</edit>

<edit #2>Sloppy programming on my part, 0 % 3 == 0, so I need to initialize $i at 1</edit #2>

<final edit>And an error in the friend's PHP: he never re-initialized $sImage, which I failed to notice. whoops again.</edit>

Posted: Tue Aug 13, 2002 9:58 am
by llimllib
In case anyone else was confused, Rawhead sent me this:
//ok why this line?
$i = 0;
while($rsImage=mysql_fetch_array($rsImages, MYSQL_ASSOC)){
$sImage.="<a href=\"images/upload/".$rsImage["filename"]."_large.jpg\" target=_blank><img src=\"images/upload/".$rsImage["filename"].".jpg\"></a> ";
//what is this doing?
if($i % 3 == 0)
print '<br>';
//and why this line
$i++;
}
to which I responded:


First, I'll explain the strategy. We want to loop through the results, and every third picture, put into html a <br> character, signifying a line break.

Now, I'll go through it line by line:

Code: Select all

$i = 1;
initializes a variable $i. A variable named 'i' is often used to signify a loop counter, which is what we're using it for here.

Code: Select all

while($rsImage=mysql_fetch_array($rsImages, MYSQL_ASSOC))&#123;
Here your friend's code pulls rows from the database. In english, it says: 'Until you run out of pictures, pull one out of the database'

Code: Select all

$sImage.="<a href="images/upload/".$rsImage&#1111;"filename"]."_large.jpg" target=_blank><img src="images/upload/".$rsImage&#1111;"filename"].".jpg"></a> ";
This line sets up HTML to display the image with a link to image_large.jpg. I removed the <br>, because that's what adds the line break, making things display vertically.

Code: Select all

if($i % 3 == 0)
This line says: 'if the remainder of $i divided by 3 (remember that $i counts our loops) is equal to zero, do the code below.'

Code: Select all

$sImage .= '<br>';
If $i % 3 (% means 'modulus' and signifies the remainder of a division) was equal to zero, this line only is executed, concatenating a <br> to the variable $sImage every 3rd time through the loop. Please note that I changed this line in this PM, it was incorrect before :) sorry

Code: Select all

$i++;
Now that we've gone through our loop, we add 1 to the loop counter.

Code: Select all

$sImage = '';
Finally, we re-initialize $sImage so that we can do it all over again without repeating images.

And that's it - nothing complicated, just basic math. Experiment with it, try to break it then make it work again, and you'll start learning. I also recommend the manual at php.net, phpcomplete.com, and anywhere else you can find a tutorial to start learning this stuff. Once you do, you'll get hooked like everyone else in here, I'm sure :).