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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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