Jump to content


Photo

returning an array from a function


  • Please log in to reply
3 replies to this topic

#1 bidgeir

bidgeir
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 16 October 2006 - 07:02 PM

Hi.

I am trying to call a function inside a while loop which returns a array.  The script seems to stop running when I call the function and I dont know why. Here is part of my code:

The bold code is where I call my function.

while ($row=mysql_fetch_array($result))
{


$types = $row['type'];

echo '<h3>' . $types . '</h3>';

$ar = getProduct($types);

echo "bla";
foreach($ar as $value)
{
echo "<tr height='30'><td width='150'>" . $value . "</td><td width='50'>&nbsp;</td><td width='50'>
9 tommur</td><td width='50'><input type='checkbox' name='9inch'></td><td width='50'>12 tommur</td>
<td width='50'><input type='checkbox' name='12inch'></td><td width='50'>16 tommu</td>
<td width='50'><input type='checkbox' name='16inch'></td><td width='50'>Fjöldi</td><td width='50'><input type='text' name='qt'></td></tr>";
}
$counter++;
}

Here is the function:

function getProduct($type)
{

include 'db.php';
if (!($connection = @ mysql_pconnect($hostName,
                                        $username,
                                        $password)))
        die("Could not connect to database");



    if (!mysql_select_db($databaseName, $connection))
        showerror();

$theType = $type;
$query = "SELECT name FROM products WHERE type='$theType'";

if (!($result = @ mysql_query($query1, $connection)))
        showerror();

$row=mysql_fetch_array($result);

return ($row);
}

The script shows everything until I call the function. I added echo "bla"; to see if anything happend after the call, but it doesnt.

Hope someone can help,

Ásgeir

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 October 2006 - 07:38 PM

I get the impression that expect your function to return an array of several products. It would only do that if you have a poorly designed table where a record contains something like

type | product1 | product2 | product3

as it will return the fields from the first record found.

However, as it there is at least one error and quite a bit of unnecessary code.

1 ) you don't need the include and connection stuff unless you're searching in a different database

2 ) you pass "$type" to the function but search for "$theType"

EDIT Sorry, missed this line "$theType = $type;"
and you have $query and $query1
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 16 October 2006 - 07:47 PM

(sidenote: bidgeir, can you wrap your code in "code" tags just to make it a little easier to read?)
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 October 2006 - 07:50 PM

Try
<?php
function getProduct($type)
   {
      $query = "SELECT name FROM products WHERE type='$type'";
      $res_array = array();
      
      if (!($result = @ mysql_query($query, $connection)))
           showerror();
      
      while ($row = mysql_fetch_array($result))
      {
        $res_array[] = $row['name'];
      }
      
      return ($res_array);
   }
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users