[SOLVED]Undefined variable

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
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

[SOLVED]Undefined variable

Post by pookie62 »

Hi,
I've got a dropdownbox in which I can select an item.
But the first time I open this page I get an error that a variable is undefined.
When I selct an item the error is no longer there.
How can I echo a default value here ?
This the code :

Code: Select all

<?php
$sql = 'SELECT Naam FROM wedstrijd';
$result = mysql_query($sql, $ASNtest) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
    $wedstrijdnaam[] = $row['Naam'];
}

if (true === isset($_POST['Search']))
{
    $where_clause = '';
    // handle search
    if (true === isset($_POST['Naam']))
    {
        // Check for search by Wedstrijd
        $w_naam = $_POST['Naam'];
        if (0 < strlen($w_naam))
        {
          $where[] = ' Naam = ''' . $w_naam . '''';
        }

        if (true === is_array($where))
        {
            $where_clause = implode(' AND ', $where);
        }
    }
?>
Last edited by pookie62 on Sun Dec 26, 2004 6:53 am, edited 1 time in total.
vietnamese
Forum Newbie
Posts: 7
Joined: Tue Dec 21, 2004 2:33 am

Post by vietnamese »

I think you should post that error string?
If you want to echo a default value in a select box, you can use:

Code: Select all

<?php
	$array_value = array("a","b","c","d");
	$default = "c";
	$select_string = "<select name='select'>\n";
	foreach ($array_value as $value){
		if ($value == $default)
			$select_string .= "<option name='$value' selected>$value</option>\n";
		else
			$select_string .= "<option name='$value'>$value</option>\n";	
	}
	$select_string .= "</select>\n";
	echo $select_string;
?>
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Hi Vietnamese,
Thanks for your quick reply !
This error message isn't to exiting :
Notice: Undefined variable: w_naam in example.php

How should I implement this in my code ?
('m a real newbie...)
Thanks again !
vietnamese
Forum Newbie
Posts: 7
Joined: Tue Dec 21, 2004 2:33 am

Post by vietnamese »

You try chang your code as:

Code: Select all

<?php
$sql = 'SELECT Naam FROM wedstrijd';
$result = mysql_query($sql, $ASNtest) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
    $wedstrijdnaam[] = $row['Naam'];
}

if (isset($_POST['Search']))
{
    $where_clause = '';
    // handle search
    if ( isset($_POST['Naam']))
    {
        // Check for search by Wedstrijd
        $w_naam = $_POST['Naam'];
        if (0 < strlen($w_naam))
        {
          $where[] = ' Naam = ''' . $w_naam . '''';
        }

        if (is_array($where))
        {
            $where_clause = implode(' AND ', $where);
        }
    }
?>
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Makes no difference..
Let me provide the complete code, maybe it helps understand you better..

Code: Select all

<?php require_once('../../Connections/ASNtest.php'); ?>
<?php
mysql_select_db($database_ASNtest, $ASNtest);
//These arrays will probably be bult from your database tables like

$sql = 'SELECT Naam FROM wedstrijd';
$result = mysql_query($sql, $ASNtest) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
    $wedstrijdnaam[] = $row['Naam'];
}
if (isset($_POST['Search']))
{
    $where_clause = '';
    // handle search
    if ( isset($_POST['Naam']))
    {
        // Check for search by Wedstrijd
        $w_naam = $_POST['Naam'];
        if (0 < strlen($w_naam))
        {
          $where[] = ' Naam = ''' . $w_naam . '''';
        }

        if (is_array($where))
        {
            $where_clause = implode(' AND ', $where);
        }
    }

    /*
        build your query to search the appropriate talbe(s)
        and add the $where_clasuse to it, something like */

          

echo 'Uitslag opzoeken voor ' . $w_naam;
}


//Query Klassement

$query_klassement = "SELECT `klasse`.`Klasse`, `deelnemer`.`Naam` AS `Deelnemer`, `deelnemer`.`Vereniging` AS `Vereniging`,`deelnemer`.`URL` AS `URL`, `wedstrijd`.`Naam` AS `Wedstrijd`, `wedstrijd`.`Datum`, `wedstrijd`.`Plaats` AS `Plaats`, TRUNCATE(`score`.`Tijd` ,2) AS `Tijd` FROM `deelnemer` INNER JOIN `inschrijving` ON (`deelnemer`.`Id` = `inschrijving`.`DeelnemerId`) INNER JOIN `wedstrijd` ON (`inschrijving`.`WedstrijdId` = `wedstrijd`.`Id`)  INNER JOIN `klasse` ON (`inschrijving`.`KlassId` = `klasse`.`Id`) INNER JOIN `score` ON (`inschrijving`.`Id` = `score`.`InschrijvingId`) WHERE `wedstrijd`.`Naam` = ('$w_naam') ORDER BY `klasse`.`Klasse`, `Tijd` ASC";
   
	$result = mysql_query($query_klassement) or die("Error: " . mysql_error());

// Begin your table outside of the array
echo "<table width="85%" border="1" cellpadding="2" cellspacing="2">
    <tr>
    	<td><b>Nr</b></td>
        <td><b>Klasse</b></td>
        <td><b>Naam</b></td>
        <td><b>Vereniging</b></td>
        <td><b>Wedstrijd</b></td>
        <td><b>Tijd</b></td>
        <td><b>Percentage</b></td>
                
    </tr>"; 
    
// Define your colors for the alternating rows

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$row_count = 1; 



// Start Form
$html_form = '<form action="' . $_SERVER[PHP_SELF] . '" method="POST">';

// Wedstrijd drop-down
$html_form .= 'Selecteer Wedstrijd <br />';
$html_form .= '<select name="Naam">';
$html_form .= '<option selected value="">Alle Wedstrijden</option>';
foreach ($wedstrijdnaam as $w_naam)
{
    $html_form .= '<option>' . $w_naam . '</option>';
}
$html_form .= '</select"> <br /><br >';

// Search button
$html_form .= '<input type="submit" name="Search" value="Bekijk" />';
echo $html_form; 


while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$row_count = $row_count;
	$Klasse = $line["Klasse"];
    $Naam = $line["Deelnemer"];
    $Vereniging = $line["Vereniging"];
    $w_naam = $line["Wedstrijd"]; 
    $Tijd = $line["Tijd"];
    $URL = $line["URL"];

        
    
    /* Now we do this small line which is basically going to tell
    PHP to alternate the colors between the two colors we defined above. */

    $row_color = ($row_count % 2) ? $color1 : $color2; 
    
    
     // Echo your table row and table data that you want to be looped over and over here. 
  
echo "<tr>
    <td width="5" bgcolor="$row_color">$row_count</td>
    <td width="10" bgcolor="$row_color">$Klasse</td>
    <td width="200" bgcolor="$row_color">$Naam</td>
    <td width="160" bgcolor="$row_color" nowrap><a href="$URL">$Vereniging</td>
    <td width="200" bgcolor="$row_color">$w_naam</td>
    <td width="80" bgcolor="$row_color" nowrap>$Tijd</td>
    </tr>"; 
    // Add 1 to the row count

    $row_count++;
	}

//break before paging
echo "<br />";
/* Closing connection */
mysql_close($ASNtest);
?>
vietnamese
Forum Newbie
Posts: 7
Joined: Tue Dec 21, 2004 2:33 am

Post by vietnamese »

at line 37 and line 43, you use variable $w_naam but in first time, it not define.
if $w_naam contant a empty string and that is true in your web, you can insert at line 5 a string : $w_naam = "";
if w_naam mush contant a string at leat a char, you mush use command
if ((isset($w_naam))&&($w_naam != "")) {
// use $w_naam
}
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

$w_naam = "";
This did it ! Thanks a lot !

Other question:
You see I'm ORDER BY `Tijd` this is the smallest value of five (every competitor has five scores per Match (dutch =wedstrijd).
How can I display these scores in a % column ?
I do have the column allready in display but no values are in it, because I wouldn't know where to start with this calculation.
The smallest score is winner = 100% other values have to be related to this. e.g.

Rank Klasse Name Tijd Percentage
1. OKP John 12,00 100%
2. OKP Hank 24,00 50%
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Anyone ideas ??
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php

$base = 12; // score of the person you want others compared to

$other_score = 24; // score of someone else

$other_score_percentage = ($base / $other_score) * 100;


?>
this may result in floats, so you will want to use round() or somthing similar to keep the percentages from getting too precise
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Hi rehfeld,
Thanks for your reply..
Okay, but now $base is hardcoded.
Can you show me how to code to look at the top value ?
Output now is ORDER BY Tijd, so the smallest score is at the top of the list.
This should become the $base value..
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

do something like this in your loop

Code: Select all

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $score = $line['Tijd'];

    if (!isset($base)) {
        $base = $score; // set base to the score of this user
    }


}
the first time it loops, $base wont have been set yet, so it will set it
on every loop after, it will remain the same value because the !isset() wont be true anymore

does that help?
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Thanks rehfeld,
going to try this and let you know.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Worked it out, thanks again guys !!
Post Reply