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? Link to comment https://forums.phpfreaks.com/topic/41661-solved-assigning-the-maximum-value-to-the-minimum-and-so-fourth/ 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 (,)? Link to comment https://forums.phpfreaks.com/topic/41661-solved-assigning-the-maximum-value-to-the-minimum-and-so-fourth/#findComment-201864 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) Link to comment https://forums.phpfreaks.com/topic/41661-solved-assigning-the-maximum-value-to-the-minimum-and-so-fourth/#findComment-201876 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--; } ?> Link to comment https://forums.phpfreaks.com/topic/41661-solved-assigning-the-maximum-value-to-the-minimum-and-so-fourth/#findComment-201884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.