e1seix Posted June 2, 2008 Share Posted June 2, 2008 I have the following loop: $go=mysql_query("SELECT * FROM admin WHERE sku>1000 AND sku<9999 ORDER BY sku")or die(mysql_error()); $numrow1=mysql_num_rows($go); while($row = mysql_fetch_array( $go )) { print $row[sku]."<br />"; } which of course allows me to see all the skus that are already in use in my database. However, to do it the other way around... how would I rejig that code to show me which skus (which is always a number between 1000 and 9999 AREN'T in use, instead of having to scan the list to find ones that are missing, if you see what I mean. many thanks, Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 $go=mysql_query("SELECT * FROM admin WHERE sku>1000 AND sku<9999 ORDER BY sku")or die(mysql_error()); $numrow1=mysql_num_rows($go); while($row = mysql_fetch_array( $go )) $exists[] = $row['sku']; for ( $i = 1000; $i <= 9999; $i++ ) if ( !in_array( $i, $exists ) ) $notExists[] = $i; print_r($notExists); I wouldn't recommend calling this script often though, as there are a ton of loops. There may be a more efficient way of doing this Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Hmm, can't think of an easy way to do it all though mysql, but you could do this: <?php $sql = "SELECT sku FROM admin WHERE sku BETWEEEN 1000 and 9999"; $result = mysql_query($sql) or die(mysql_error()); $taken = array(); $possible = range(1000,9999); while($row=mysql_fetch_row($result)){ $taken[] = $row[0]; } $unused = array_diff($range,$possible); echo '<pre>'.print_r($unused,1).'</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
e1seix Posted June 2, 2008 Author Share Posted June 2, 2008 First example worked, the second one didn't. Many thanks, Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 2, 2008 Share Posted June 2, 2008 Yeah, mine should have been: <?php $sql = "SELECT sku FROM admin WHERE sku BETWEEEN 1000 and 9999"; $result = mysql_query($sql) or die(mysql_error()); $taken = array(); $possible = range(1000,9999); while($row=mysql_fetch_row($result)){ $taken[] = $row[0]; } $unused = array_diff($possible,$taken); echo '<pre>'.print_r($unused,1).'</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
craygo Posted June 2, 2008 Share Posted June 2, 2008 run another loop from 1000 to 9999 and compare it <?php $go=mysql_query("SELECT * FROM admin WHERE sku>1000 AND sku<9999 ORDER BY sku")or die(mysql_error()); $numrow1=mysql_num_rows($go); while($row = mysql_fetch_array( $go )) { $skus[] = $r['sku']; print $row[sku]."<br />"; } for($i=1000; $i<=9999; $i++){ if(!in_array($i, $skus)){ echo $i."<br />"; } } ?> Ray EDIT: late again!! Quote Link to comment Share on other sites More sharing options...
e1seix Posted June 2, 2008 Author Share Posted June 2, 2008 Actually, as opposed to displaying the results in an array, is there a way of doing it for each result as a single variable so I can utilise them in turn. Just a thought. It looks as though Craygo's answer would be appropriate, but the code itself doesn't perform correctly. It just lists every number between 1000 & 9999. Hmm... Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 An array is, in a sense, a variable containing many variables. I'm not sure what you want to do, but if you can do it with a variable, chances are you can do it with an array entry. Quote Link to comment Share on other sites More sharing options...
craygo Posted June 3, 2008 Share Posted June 3, 2008 That is because I left the print of the shu in <?php $go=mysql_query("SELECT * FROM admin WHERE sku>1000 AND sku<9999 ORDER BY sku")or die(mysql_error()); $numrow1=mysql_num_rows($go); while($row = mysql_fetch_array( $go )) { $skus[] = $r['sku']; } for($i=1000; $i<=9999; $i++){ if(!in_array($i, $skus)){ echo $i."<br />"; } } ?> Ray 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.