...

View Full Version : PHP loops problem.



edawg
01-29-2007, 09:10 PM
Hi, I'm trying to update a current pictures site from html into php with a mysql backend, im pretty new to php and mysql and have run into a problem I cant seem to remedy??, any help would be great!.

I have set up the database, and in the database I have 2 tables so far, one is for the pictures categories which has the names of the categories, and the urls, and the other is for the pictures which has the title of the pictures, and filenames and description, so I linked them both with a associated primary/foreign key relationship.

Now my problem is with a section on my site I would like the latest pictures posted to be shown, so I used a href so you can click on anyone and it will take you to that pictures category, the only problem is I get the latest pictures showing up fine, but when I put the mouse on them the url is the same all the way down, rather than change for each individual picture category, it stays the same link?, ive tried as much as I know so far, but still the same problems?, what can I do?.


mysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );

$query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC";

$sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());
$row_sPictures = mysql_fetch_assoc($sPictures);
$totalRows_sPictures = mysql_num_rows($sPictures); <-- This part works fine
?>

<?php
$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id";

$sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());
$row_sCategories = mysql_fetch_assoc($sCategories);
$totalRows_sCategories = mysql_num_rows($sCategories);
?>

<?php do { ?><a href="<?php echo $row_sCategories['sc_url'];?>"> <-- This is where the problem is??.

<img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img>[/url]

<?php } while ($row_sPictures = mysql_fetch_assoc($sPictures));


mysql_close();?>

Any help would be great, im pulling my hair out with this!!!!!!.
Other forums have not been too helpfull!!.

angst
01-29-2007, 09:25 PM
just a note:
if your code was cleaner more people would look at it.



<%
mysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );

$query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC";
$sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());
$row_sPictures = mysql_fetch_assoc($sPictures);
$totalRows_sPictures = mysql_num_rows($sPictures);

$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id";
$sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());
$row_sCategories = mysql_fetch_assoc($sCategories);
$totalRows_sCategories = mysql_num_rows($sCategories);

while ($row_sPictures = mysql_fetch_array($sPictures)) {
echo "<a href='" . $row_sCategories['sc_url'] . "'><img src='pictures/" . $row_sPictures['sp_filename_TN'] . "' alt='" . $row_sPictures['sp_ALT'] . "' /></a>";
}

mysql_close();
?>


try that.

martialtiger
01-29-2007, 09:26 PM
Why not do something like this in lieu of the code you provided.



<?php
$qry = "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
$rslt = @mysql_query($qry, $connDB) or die(mysql_error());
$total_rows = mysql_num_rows($rslt);
while ($row = mysql_fetch_assoc($rslt)){
echo "<a href=\"$row['sc_url']\"><img src=\"pictures/$row['sp_filename_TN']\" alt=\"$row['sp_ALT']\"></a>";
}
mysql_close($connDB);
?>

angst
01-29-2007, 09:29 PM
haha, i didn't even wanna think that much about it,
sort of hurts just to look at ;-P

but, practice makes perfect

martialtiger
01-29-2007, 09:33 PM
haha, i didn't even wanna think that much about it,
sort of hurts just to look at ;-P

but, practice makes perfect
Couldn't agree with you more, Angst. My intial response was directed to eDawg and didn't mean to seem like I was commenting on your suggestion.

eDawg,
As Angst suggested please try to clean up your code before just copying and pasting. This will help others when then attempt to look through your code to help.

Good luck!

edawg
01-29-2007, 09:39 PM
Why not do something like this in lieu of the code you provided.



<?php
$qry = "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
$rslt = @mysql_query($qry, $connDB) or die(mysql_error());
$total_rows = mysql_num_rows($rslt);
while ($row = mysql_fetch_assoc($rslt)){
echo "<a href=\"$row['sc_url']\"><img src=\"pictures/$row['sp_filename_TN']\" alt=\"$row['sp_ALT']\"></a>";
}
mysql_close($connDB);
?>


Hi, and thanks for your help!!!, I will clean up code in the future, because ill probably be in here alot.

I just tried the code you gave me, but get this error,

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING in file/latest_pictures.php on line 6

:eek:

martialtiger
01-29-2007, 09:46 PM
Try this


<?php
$qry = "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
$rslt = @mysql_query($qry, $connDB) or die(mysql_error());
$total_rows = mysql_num_rows($rslt);
while ($row = mysql_fetch_assoc($rslt)){
echo "<a href=\"" . $row['sc_url'] . "\"><img src=\"pictures/" . $row['sp_filename_TN'] . "\" alt=\"" . $row['sp_ALT'] . "\"></a>";
}
mysql_close($connDB);
?>


If that doesn't fix it you may need to insert the codes 1-10 of lastest_pictures.php just so we can take a look further into it.

angst
01-29-2007, 09:47 PM
make sure to clean up any white spaces ( blank spaces ).
the code looks ok otherwise.

edawg
01-29-2007, 10:03 PM
All I can say is thank for both of your help!!!, I actually modified it some more into this -->


<?php
$qry = "SELECT sp.sp_filename_TN, sp.sp_ALT, sc.sc_url FROM site_categories sc, site_pictures sp WHERE sp.sc_id = sc.sc_id ORDER BY sp.sp_timestamp ASC";
$rslt = @mysql_query($qry, $connDB) or die(mysql_error());
$row_result = mysql_fetch_assoc($rslt);
$total_rows = mysql_num_rows($rslt);?>
<?php
do { ?><a href="<?php echo $row_result['sc_url']; ?>"><img src="pictures/<?php echo $row_result['sp_filename_TN'];?>" alt="<?php echo $row_result['sp_ALT']; ?>"></a>
<?php } while ($row_result = mysql_fetch_assoc($rslt)) ;

mysql_close($connDB);

?>

And it now works!!:D , I could not of done it without your help!!, and I learned along the way!!.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum