Current location: Hot Scripts Forums » Programming Languages » PHP » limiting foreach()


limiting foreach()

Reply
  #1 (permalink)  
Old 02-26-04, 12:08 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
limiting foreach()

Okay, so im using foreach() to pull the info from my database and turning them into variables ..however, when there's nothing in the database, i get an error. Its probably as plain as day, but whats the code to tell it, to pull from the database only if theres 1 or more rows..

Thanks!
Reply With Quote
  #2 (permalink)  
Old 02-26-04, 01:02 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

mysql_num_rows() to test if it's an empty set or is_array() to test if it's an array before running your foreach loop might help?

HTH.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 02-26-04, 05:20 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Blaw, is_array worked perfectly, i tried mysql_num_rows before i posted this topic, it gave me more errors for some reason. But anyway, thanks again!

*edit*

Ohhh, mkay, it worked perfectly at getting rid of the error, but now when theres data in their, it wont pull it, heres my code ...

PHP Code:

    $appquery $db->query("SELECT * FROM $table_applied");

      if (
is_array($appquery)) {
        foreach(
$db->fetch_array($appquery) as $key => $val) {
        $
$key $val;
        
$APPLIED[$key] = $val;
        }
    } 

Last edited by simone; 02-26-04 at 05:25 AM.
Reply With Quote
  #4 (permalink)  
Old 02-26-04, 08:58 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again,

Your code was somewhat different from what I assumed it to be... Anyway, how about using if(!empty($appquery)) or even if($appquery) then, instead of is_array()? is_array() doesn't work because before fetching it, it is not an array yet, but resource.

I don't use PEAR's DB class, so I'm not too sure of this, but there must be a method that works in the same way as mysql_num_rows() does, too, which you might want to check out.
__________________
Blavv =|
Reply With Quote
  #5 (permalink)  
Old 02-26-04, 06:51 PM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

     $appquery $db->query("SELECT * FROM $table_applied"); 

  if(!empty(
$appquery));
      else {
        foreach(
$db->fetch_array($appquery) as $key => $val) { 
        $
$key $val
        
$APPLIED[$key] = $val
        } 
    } 
That does the exact same thing as the is_array() ..this is really bugging me, i also tried the if(@appquery) and it showed the info, but again when there was nothing in the database it gave me the error.
Reply With Quote
  #6 (permalink)  
Old 02-26-04, 07:39 PM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
ahhh, i figured it out, i was putting my mysql_num_rows in wrong ...heres how it worked out.

PHP Code:

$appquery $db->query("SELECT * FROM $table_applied"); 

  
$approws $db->num_rows($appquery);
      if(!
$approws);
         else { 
           foreach(
$db->fetch_array($appquery) as $key => $val) { 
              $
$key $val
                
$APPLIED[$key] = $val
        }
           }; 
Thanks for your help Blaw!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Formmail Script Fix - Cant display multiple list selections.. zamen PHP 3 05-02-04 09:59 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
Limiting search results by offset johno PHP 1 11-20-03 09:21 AM


All times are GMT -5. The time now is 09:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.