Page 1 of 1

returning values

Posted: Mon Jan 08, 2007 6:58 am
by kpraman
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

     I have written a function to fetch data and return values.

Code: Select all

function fetchAllData($cond,$table)
	    {
		   //echo "SELECT * FROM $table";
		   $query=mysql_query("SELECT * FROM $table $cond");
				 while($data=mysql_fetch_array($query))
				   {
					  $datas[]=$data;
				   }
				 return $datas;
		}




$membrsData=fetchAllData("","members");
And i am trying to populate drop down list using foreach statement.

Code: Select all

foreach($membrsData as $membrsDatas)
   {

    	      if($membrsDatas['Country']==$country['countryName'])
	          $countrys.="<option selected>$membrsDatas[Country]</option>";
		  else
	          $countrys.="<option>$membrsDatas[Country]</option>";


   }


The above code works fine if there are more than 1 row. it does not work if there is only one row.

Thanx





feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 08, 2007 8:34 am
by Kieran Huggins
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


foreach gives a warning?

You could use:

Code: Select all

$query=mysql_query("SELECT * FROM $table $cond");
   while($row=mysql_fetch_array($query)){
      if($row['Country']==$country['countryName']){
         $countrys.='<option selected="selected">'.$row['Country'].'</option>';
      }else{
         $countrys.='<option>'.$row['Country'].'</option>';
      }
   }
Or even just an for($i=0,$n=count($membrsData);$i<n;$i++){} loop instead.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 08, 2007 8:50 am
by feyd
kpraman, you have 82 posts so far. Moderators shouldn't have to edit your posts so often, if ever, at this point.

Shape up.

Posted: Mon Jan 08, 2007 8:52 am
by Kieran Huggins
Did I forget my code tags? :oops:

The shame is more than I feared it would be!

Posted: Mon Jan 08, 2007 8:55 am
by feyd
You used

Code: Select all

instead of

Code: Select all

. You're not in the hot Kieran.

Posted: Mon Jan 08, 2007 9:03 am
by kpraman
i want a general function which can return the values when we pass tablename, data etc,

the problem is when the table is having more than 1 row, there is no problem with the function, if it is only one row then i am getting problem, (this is because i am using a foreach statement). I wanted to know how i can modify that function.

Sorry, i keep forgetting about the formating

Thanx

Posted: Mon Jan 08, 2007 9:19 am
by Kieran Huggins
instead of foreach() use:

Code: Select all

for($i=0,$n=count($membrsData);$i<n;$i++){/* do stuff here */}
instead - it won;t throw an error like foreach if you only have one row.

Alternatively, you could also mute the output of foreach with an @:

Code: Select all

@foreach(...){....}
But that's the lazy way.

Posted: Tue Jan 09, 2007 2:23 am
by kpraman
it seems i did not explain my problem clearly.

once i get the return values

Code: Select all

function fetchAllData($cond,$table) 
            { 
                   //echo "SELECT * FROM $table"; 
                   $query=mysql_query("SELECT * FROM $table $cond"); 
                                 while($data=mysql_fetch_array($query)) 
                                   { 
                                          $datas[]=$data; 
                                   } 
                                 return $datas; 
                } 

$membrsData=fetchAllData("","members");
if there were only one row in the database, i have to use $membersData[0].

if there were more than one row in the database, i have to use $membersData

to retrive values. When i want to populate drop down, i get wrong data

Posted: Tue Jan 09, 2007 7:56 am
by feyd
Maybe you should use count() to detect the presence of only one row.