Page 1 of 2

return display full results

Posted: Sun Apr 23, 2006 2:08 am
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

Posted: Sun Apr 23, 2006 2:15 am
by feyd
$display[]

Posted: Sun Apr 23, 2006 2:18 am
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

Posted: Sun Apr 23, 2006 2:25 am
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.

Posted: Sun Apr 23, 2006 3:15 am
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 :)

Posted: Sun Apr 23, 2006 4:34 am
by feyd
What have you tried?

Posted: Sun Apr 23, 2006 4:56 am
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 :)

Posted: Sun Apr 23, 2006 8:45 am
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.

Posted: Sun Apr 23, 2006 10:51 am
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:

Posted: Sun Apr 23, 2006 3:57 pm
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; 
    }

Posted: Sun Apr 23, 2006 4:08 pm
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..!

Posted: Mon Apr 24, 2006 6:17 am
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

Posted: Mon Apr 24, 2006 4:37 pm
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.

Posted: Mon Apr 24, 2006 5:00 pm
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());

Posted: Mon Apr 24, 2006 5:36 pm
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";
}
?>