Jump to content

Do While Loops Problem


edawg

Recommended Posts

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?.

CODE #######################

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);  [color=red]<-- This part works fine[/color]
?>

<?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'];?>"> [color=red]<-- This is where the problem is??.[/color]

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

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


mysql_close();?>

Any help would be great, im pulling my hair out with this!!!!!!. >:(

Link to comment
Share on other sites

When you post here, wrap your code in code tags using the # button above the smileys

You are using a do..while loop, and I have always seen a while...do loop used here. Try the code I have below. I replaced your do with the while portion you had at the end

[code]
<?php
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 while ($row_sPictures = mysql_fetch_assoc($sPictures){ ?>
<a href="<?php echo $row_sCategories['sc_url'];?>">

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

<?php }


mysql_close();?>

[/code]
Link to comment
Share on other sites

@chronister: the do..while loop is technically fine.  The difference between do..while and while... is that the stuff inside the do...while loop will always be executed at least once: the first time. With the while... loop, it will only execute (even the first time) if the condition is met.

@edawg: when you execute a query, it returns a result source.  You must then use a function to grab the info from the result source (mysql_fetch_assoc is one such function).  mysql_fetch_assoc grabs the info one row at a time, so you that's why you loop the actual function call.  example:

[code]
$sql = "select column1 from table";
$result = mysql_query($result);

while ($list = mysql_fetch_assoc($result)) {
  echo $list['column1'] . "<br/>";
}
[/code]
Link to comment
Share on other sites

This is the problem page -->>  [url=http://www.cellphonepictoria.com/test/index.php]http://www.cellphonepictoria.com/test/index.php[/url]
The problem is with the right hand bar "Latest Pictures", as you hoover over them, I want the pictures to point to there own category url, rather than have the same link as the first picture in the list.

And I tried what you said about the nesting, but was not too sure how to do this, so i tried this with the code?, is this what you meant?

[code]<?php
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";
// Print $query_sCategories;
$sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());
$row_sPictures = mysql_fetch_assoc($sPictures);
$totalRows_sPictures = mysql_num_rows($sPictures);
?>
<?php
$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id";
// Print $query_sCategories;
$sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());
$row_sCategories = mysql_fetch_assoc($sCategories);
$totalRows_sCategories = mysql_num_rows($sCategories);
?>
<?php do { ?>
<?php do { ?><a href="<?php echo $row_sCategories['sc_url'];?>">
<?php } while ($row_sCategires = mysql_fetch_assoc($sCategories)); ?>
<img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img></a>
<?php } while ($row_sPictures = mysql_fetch_assoc($sPictures));

mysql_close();

?>[/code]

I really appreciate all your help too!!!!.
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.