writing functions

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

Post Reply
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

writing functions

Post by deejay »

Hi

As you'll be able to tell from the following message I'm a complete novice at PHP.
I have just managed to make my first script that write information to a mySQL database and I can also retrieve it. In the table for this database is information for a menu bar and what I'd like to do is for the script to place all buttons in a list. I have tried to do this in a function. Heres my code (dont laugh)

<?php
include_once("connection/connection.php");

$result = mysql_query("SELECT * FROM mainMenu",$db);

$imageURL = mysql_result($result,0,"image");
$imageALT = mysql_result($result,0,"alt");
$imageLink = mysql_result($result,0,"link");



function writeMenu($imageLink, $imageURL, $imageALT)
{
$num_rows = mysql_num_rows($result);
{
printf('<table width="136" border="0" cellspacing="0" cellpadding="0">');

if ($num_rows > 0){
for($i = 0; $i < $num_rows; $i++)
printf('<tr> <td width="857" height="25">');
printf("<a href=\"$imageLink\"><img src=\"$imageURL\" width=\"136\" height=\"25\" alt=\"$imageALT\"> </a> \n");
printf('</tr> <tr>');
}
}
}
?>

and i call up the fuction with

<?php
writeMenu()
?>


and get back the error
Missing argument 1(to 3) for writemenu() in public_html/index.php on line 13

which is this line
function writeMenu($imageLink, $imageURL, $imageALT)

and the error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in public_html/index.php on line 15

which is
$num_rows = mysql_num_rows($result);


any clues on what I have done would be gratefully received. Thanks.

Deej
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Try defining the variables INSIDE the function:

function writeMenu($imageLink, $imageURL, $imageALT)
{
$result = mysql_query("SELECT * FROM mainMenu",$db);

$imageURL = mysql_result($result,0,"image");
$imageALT = mysql_result($result,0,"alt");
$imageLink = mysql_result($result,0,"link");

$num_rows = mysql_num_rows($result);
{
printf('<table width="136" border="0" cellspacing="0" cellpadding="0">');

if ($num_rows > 0){
for($i = 0; $i < $num_rows; $i++)
printf('<tr> <td width="857" height="25">');
printf("<a href=\"$imageLink\"><img src=\"$imageURL\" width=\"136\" height=\"25\" alt=\"$imageALT\"> </a> \n");
printf('</tr> <tr>');
}
}
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You have defined the function with three arguments so you need to pass those in the function call:

Code: Select all

<?php 
writeMenu($imageLink, $imageURL, $imageALT); 
?>
Or if the variable names were not going to change you could change the function and make them globals:

Code: Select all

function writeMenu() 
{ 
    global $imageLink, $imageURL, $imageALT;
    global $result;
    ....
}
In PHP functions have no idea about any variables (except the autoglobals $_POST, $_GET et al.) outside of the function so you have to either pass them as arguments or define them as global within the function.

For more information:
http://www.php.net/manual/en/functions.php

Mac
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

holy crap, what the hell was i thinking?

Sorry, i've just been working on a huge Auth class for my site. Like 1000 lines long. 20+ functions.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

_

Post by deejay »

Firstly thank you for replying to my post so quickly. Normally I usually end up answering my own posts on messageboards as a kind of self therapy before others logon and read my mad ramblings.

There were a few things wrong with my script. I think I understand the arguments problem now, where if the variable is coming from a database every time the variable can be made global.

After fixing this there is now an error with the array; the function at this point looks like this

function writeMenu()
{
global $imageLink, $imageURL, $imageALT;
global $result;
$num_rows = mysql_num_rows($result);
{
printf('<table width="136" border="0" cellspacing="0" cellpadding="0">');

if ($num_rows > 0){
for($i = 0; $i < $num_rows; $i++)
printf('<tr> <td width="857" height="25">');
printf("<a href=\"$imageLink\"><img src=\"$imageURL\" width=\"136\" height=\"25\" alt=\"$imageALT\"> </a> \n");
printf('</td></tr> ');
}
else {
print "</table>";
}
}
}
?>


I checked the $num_rows is working by this code

echo "$num_rows Rows\n";

although it gives me the figure 5 when it should be 6!!

I am now going back to all documentation regarding using arrays, I think the line
if ($num_rows > 0){
for($i = 0; $i < $num_rows; $i++)

must be incorrect. Thank you for any help given.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Could be because $result no longer has a query.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The for loop should not be affecting the number of results returned. Try changing your function to something like:

Code: Select all

<?php

function writeMenu() 
{ 
	global $result; 
	$num_rows = mysql_num_rows($result); 
	
	if ($num_rows > 0) { 
		echo '<table width="136" border="0" cellspacing="0" cellpadding="0">'; 
		while ($row = mysql_fetch_assoc($result)) {
			echo '<tr> <td width="857" height="25">';
			echo '<a href="'.$row['link'].'"><img src="'.$row['image'].'" width="136" height="25" alt="'.$row['alt'].'" /></a>'."\n";
			echo '</td></tr>';
		}
		echo '</table>';
	} 
} 
?>
and remove the following rows from above it:

Code: Select all

$imageURL = mysql_result($result,0,"image"); 
$imageALT = mysql_result($result,0,"alt"); 
$imageLink = mysql_result($result,0,"link");
Does that work any better?

Mac
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Thanks

Post by deejay »

this works :-)

cheers



i will have to spend more time going through functions for arrays etc as its something i dont really understand
Post Reply