proud Posted March 7, 2007 Share Posted March 7, 2007 I'm trying to write a script that will allow me to reverse the maximum values with the minimum values, for example lets assume I have a table in which I have got two fields named (photo_id , photo_name ) and there are 30 photos In this table ; now I'm trying to assign the photo_id with the value equal to 1 a value of 30, and the photo_id with the value equal to 2 a value of 29 and so going on... To make it more clear: 1=30 2=29 3=28 4=27 . . . etc... So what is the code that will allow me to do this? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 where are you assigning the max to min? in another column in the database? or are you storing both in the photo_id colum, separated by a comma (,)? Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 7, 2007 Share Posted March 7, 2007 UPDATE your_table SET photo_id=((SELECT MAX(photo_id) FROM your_table) - photo_id + 1) Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 7, 2007 Share Posted March 7, 2007 maybe this is what you're looking for: <?php $sql = "SELECT photo_id FROM your_table ORDER BY ASC"; $query = mysql_query($sql); $i = mysql_num_rows($query); while($row = mysql_fetch_array($query)){ $sql = "UPDATE your_table SET photo_id = '". $i ."'"; mysql_query($sql); $i--; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.