You are here: Need help with for loop, pulling MySQL data, completely lost « PHP « IT news, forums, messages
Need help with for loop, pulling MySQL data, completely lost

Posted by Dan Trainor on 08/21/05 14:24

Hello, all -

As a pet project of mine, I've decided to write a bit of code. This is
what I have, and it's not working as expected:

if ($action == "prepareforupdate") {
@unlink("UPDATES/".$id."/9.jpg");
$query0 = "SELECT * FROM updates WHERE id=".$id."";
if (!$dbdata = mysql_query($query0)) {
echo "Can't run query: ".mysql_error();
die;
};

for ($i = 9; $i >= 0; $i--) {
$j = $i - 1;

echo "Getting ready to rename UPDATES/$id/$j.jpg to
UPDATES/$id/$i.jpg || &nbsp;&nbsp;&nbsp;&nbsp;<br />";

@rename("UPDATES/$id/$j.jpg","UPDATES/$id/$i.jpg");

$returned = mysql_fetch_assoc($dbdata);

$query1 = "UPDATE updates SET " . $i . "d = '"
.$returned[$j."d"]. "' WHERE id='" . $id . "'";

if (!mysql_query($query1)) {
echo "MySQL Error: ".mysql_error();
};

}
}


I have a database in the following format. Let's say that "d" stands
for date, and "t" stands for times, and I have ten days' worth of
archives going on:

|id|1d|1t|2d|2t|3d|3t|.......|8d|8t|9d|9t|

I'm trying to "move" the contents of the previous field to the logical
next field, so the value of field "8d" becomes "9d", the value of "7t"
becomes the value of "8t", and so on.

The problem that I'm having here is that only the first iteration of the
for loop work properly. Everything after that, is not being pulled up
properly, I suspect. This is what MySQL shows:

174 Query SELECT * FROM updates WHERE id=5
174 Query UPDATE updates SET 9d = '2005-08-21' WHERE id='5'
174 Query UPDATE updates SET 8d = '' WHERE id='5'
.....
174 Query UPDATE updates SET 1d = '' WHERE id='5'
174 Query UPDATE updates SET 0d = '' WHERE id='5'

So all in all, I think I might be a bit out of my league here, but I am
eager to learn. I think of this as more of a dynamic approach to a
situation that I'm trying to adapt to.

As always, any feedback, or flames for that matter, would be greatly
appreciated.

Thanks
-dant

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация