return display full results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

return display full results

Post by blacksnday »

Code: Select all

function my_function() 
    { 
	$sql = "SELECT something FROM table";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_array($result)) 
	{
	 $display = $row['something'];
	}
	 return $display;
    }
How do ya get that to show all results found?
Instead of just the first result found.
WITH USING a return instead of an echo
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$display[]
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

feyd wrote:$display[]
changing it to using the suggestion
(if i understood correctly)

Code: Select all

function my_function() 
    { 
        $sql = "SELECT something FROM table"; 
        $result = mysql_query($sql); 

        while ($row = mysql_fetch_array($result)) 
        { 
         $display[] = $row['something']; 
        } 
         return $display; 
    }
Makes the results show as: Array
instead of actual results
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The results are in an array. You can use a foreach or whatever loop you wish to iterate over them and create the final output you wish.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I've tried using foreach's and some other stuff with
nothing working.

I must not be doing it right
and my hair is hoping I can figure this out soon :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What have you tried?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

The below I tried in both inside and outside the sql while

Code: Select all

function my_function() 
    { 
        $sql = "SELECT something FROM table"; 
        $result = mysql_query($sql); 

        while ($row = mysql_fetch_array($result)) 
        { 
         $display = $row['something']; 
        } 
                  $show_display = array();
                  foreach ($display as $post) {
                         $show_display[] = $post;
                  }
         return $show_display; 
    }
I know the solution is an easy one once I figure it out.
Maybe the way I am trying to do the query is wrong for
this type of request?

Up until now, my code base always used Echos in functions,
but now that I am coming into more advanced development
I need to do it right and return return return :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Let's do it really slow...

1-) You want to generate an array with rows from the resultset.
2-) You know how to generate through the resultset.
3-) You know that you can add an element to an array with $array[] = $element syntax.
4) You know how to return an array.

Basically, before you start fetching from the resultset, you initialise the array that will containt the results:

$resultarray = array();

Now when you iterate over the resultset you have to add each fetched row

$resultarray[] = $row;

After you've fetched them all you can return the $resultarray.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

In your code, you are rewritting $display again! Not to mention you have two loops when you only need one.. other than that just follow timv's logic :wink:
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

ok.. i think i almost got this understanding.
Finally got the array to spit out info, but
only spitting out ARRAY

when doing print_r($show_display); it returns with
Array ( [0] => ResultName )

Code: Select all

function my_function() 
    { 
       $show_display = array();

        $sql = "SELECT something FROM table"; 
        $result = mysql_query($sql); 
        $grab = mysql_fetch_row($result);

             foreach ($grab as $row => $name) 
	{ 
                    $show_display[] = $name; 
                  }

         return $show_display; 
    }
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Ahah! I got it i think....

Code: Select all

foreach ($grab as $row => $name) 
	{ 
                    $show_display[$name] = $name; 
                    $showme = $show_display[$name];
                 }
correctly displays the results

however it still seems to only display the first found selection
and not all of them... sigh..!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Apparently you're just guessing... and hoping that things will do what you want (someday when you're lucky).

Explain us what you really want to do. Since we still haven't seen how you display something... And start reading the manual, as it explains basic stuff like iterations, loops, functions... http://www.php.net/documentation
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

timvw wrote:Apparently you're just guessing... and hoping that things will do what you want (someday when you're lucky).

Explain us what you really want to do. Since we still haven't seen how you display something... And start reading the manual, as it explains basic stuff like iterations, loops, functions... http://www.php.net/documentation
I have said what I want to do.
I want to be able to return a full result set from a sql query with using
a return instead of an echo.

If I wanted to return a full result set using an ECHO, I am full aware how to do it.

Code: Select all

function my_function() 
    { 
        $sql = "SELECT something FROM table"; 
        $result = mysql_query($sql); 

        while ($row = mysql_fetch_array($result)) 
        { 
         $display = $row['something']; 
        } 
         echo $display; 
    }

//to display all results of 'something' I would simply do
my_function();
However when doing the same thing using a return

Code: Select all

function my_function() 
    { 
        $sql = "SELECT something FROM table"; 
        $result = mysql_query($sql); 

        while ($row = mysql_fetch_array($result)) 
        { 
         $display = $row['something']; 
        } 
         return $display; 
    }

echo my_function();
it will only display the first result found.

What I want to do is be able to display all results found as
if I was using an echo, but by using a return.

I have read php docs and many many tuts on this.
I have yet to be able to understand how to do this
cause Arrays confuse the hell out of me :)
Which is why I finally came to these boards to hopefully finally figure out
this elementary task.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Code: Select all

function my_function()
    {
        $sql = "SELECT something FROM table";
        $result = mysql_query($sql);

        $retArr = array();

        while ($row = mysql_fetch_array($result))
        {
         $retArr[] = $row['something'];
        }
         return $retArr;
    }

print_r(my_function());
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
function my_function() 
{ 
    $sql = "SELECT something FROM table"; 
    $result = mysql_query($sql); 

    while ($row = mysql_fetch_array($result)) 
    { 
        $display[] = $row['something']; 
    } 
    
    return $display; 
}

// In your code set your returned array to a var, then loop that
$my_array = my_function();

foreach ($my_array as $key => $value)
{
    echo "The array key is $key and the value is $value.<br />\n";
}
?>
Post Reply