writing functions
Moderator: General Moderators
writing functions
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
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
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>');
}
}
}
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>');
}
}
}
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You have defined the function with three arguments so you need to pass those in the function call:
Or if the variable names were not going to change you could change the function and make them globals:
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
Code: Select all
<?php
writeMenu($imageLink, $imageURL, $imageALT);
?>Code: Select all
function writeMenu()
{
global $imageLink, $imageURL, $imageALT;
global $result;
....
}For more information:
http://www.php.net/manual/en/functions.php
Mac
_
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.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The for loop should not be affecting the number of results returned. Try changing your function to something like:
and remove the following rows from above it:
Does that work any better?
Mac
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>';
}
}
?>Code: Select all
$imageURL = mysql_result($result,0,"image");
$imageALT = mysql_result($result,0,"alt");
$imageLink = mysql_result($result,0,"link");Mac