Jump to content

A Looping Question


e1seix

Recommended Posts

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,

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-555763
Share on other sites

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>';
?>

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-555769
Share on other sites

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>';
?>

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-555789
Share on other sites

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!! :)

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-555790
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-555874
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/108410-a-looping-question/#findComment-556552
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.