Jump to content

A bit of help with some While loops?


xkonidis

Recommended Posts

Hello guys,

 

I'll try and explain in full. I have 2 tables in my database ( extras, extraphotos ). Table extras is holding the title, descr and extraphotos is holding the picname. Both of the have an extra row with the name of fname so i can bind these two together.

 

What I'm trying to do is to read from my DB the Title, Description and all the picname for a gallery that i'm trying to create.

 

Basically it is set up with the Cycle js plugin so I have 1 thumbnail that has extra photos ( picname ).

 

I'm struggling to get arround on this ->

 

<?php

$query="SELECT * FROM extras";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
$j=0;

while ($i < $num) {

$descr=mysql_result($result,$i,"descr");
$title=mysql_result($result,$i,"title");
$fname=mysql_result($result,$i,"fname"); 
echo '<div class="image" title="'.$title.'">';
echo '<img src="images/work/th/kolie1.jpg" alt="" />';
echo '<div class="image-label"><span>'.$title.'</span></div>';
echo '</div>';
echo '<div class="cycle">';

$query2="SELECT * FROM extraphotos WHERE fname = " .$fname;
$result2=mysql_query($query2);
$num2=mysqlnumrows($result2);

while ($j < $num2) {
$picname=mysql_result($result2,$j,"picname"); 
echo '<img src="images/work/"'.$picname.'.jpg" width="710px" height="460px" />';
$j++;
} 
echo'</div>';
echo '<div class="info">';
echo '<div class="title"><h2>'.$title.'.</h2></div> ';
echo '<div class="descr">'.$descr.'</div>';
echo '<div class="item_url">'.$fname.'</div>';
echo '</div>';
echo '</div>';
$i++;
}

?>

 

So basically my first 'while loop' is creating the divs for the thumbnails and it also adds the 'fname' in the item_url div that is needed for onClick function to show the extra images,

and i need a second loop so it can create the <img /> tags inside the thumbnails div which is inside the main one.

 

Any suggestions? I was trying to create this with two WHILE loops but no success and no idea how can I do it.

 

Thanks in advance,

Xenos K.

Link to comment
https://forums.phpfreaks.com/topic/243074-a-bit-of-help-with-some-while-loops/
Share on other sites

Thanks for the reply WebStyles, tired eyes always make mistakes i guess ^_^.

 

Again though my my second while loop for the picname returns nothing to me :S.

 

 

-------------------

 

Edit 1: I had to edit the second query as well from

$query2="SELECT * FROM extraphotos WHERE fname = " .$fname;

to

$query2="SELECT * FROM extraphotos WHERE fname = '" .$fname."'";

 

Everything works now for 1 entry, hopefully it will work for more.

 

Thanks again WebStyles, minor mistake, major disaster i guess ^_^

 

Kind regards,

Xenos K.

try something like this (untested code, but hopefully will do the same)

 

$query="SELECT * FROM extras"; // title, descr, fname
$list = array();
while($result=mysql_query($query){
$list[] = $result;
}
foreach($list as $l){
echo '<div class="image" title="'.$l['title'].'">';
echo '<img src="images/work/th/kolie1.jpg" alt="" />';
echo '<div class="image-label"><span>'.$l['title'].'</span></div>';
echo '</div>';
echo '<div class="cycle">';
// second query
        $fname = $l['fname'];
$query2="SELECT * FROM extraphotos WHERE fname = '$fname'";
while($result2=mysql_query($query2)){
	echo '<img src="images/work/"'.$result2['picname'].'.jpg" width="710px" height="460px" />';
}
// close divs
echo'</div>';
echo '<div class="info">';
echo '<div class="title"><h2>'.$title.'.</h2></div> ';
echo '<div class="descr">'.$descr.'</div>';
echo '<div class="item_url">'.$fname.'</div>';
echo '</div>';
echo '</div>';
}

 

p.s. it's still not a 'brilliant' way to do it... I don't like mysql queries inside of loops like that.

Thanks for your quick code edit,

 

I copy/pasted it but i'm getting a blank page, tried to work arround it a bit with no luck though.

 

On the other hand with my code, the 1st thumbnail shows correctly with its nested pictures, but after that all other images have no nests,

so basically it is like the second while loop works only 1 time ( for the first thumbnail ).

while($result=mysql_query($query){
$list[] = $result;
}

 

changed to:

 

while($result=mysql_query($query)){
$list[] = $result;
}

 

missing a ")" there

 

but again, no output at all, the rest of the page loads properly but the $list echoes give nothing

theres no point of making the array...Put it inside the while loop, print mysql_num_rows on $query and tel us the outpu

 

I didn't understood what you want me to do exactly :S

 

(i started coding like 2 weeks ago and some stuff still seem like mountains, bare with me if you please)

 

kind regards,

xenos k.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.