Page 1 of 1

Where's my logic going wrong ?

Posted: Tue Jul 27, 2004 8:23 am
by fccolon
OK, I've probably totally misunderstood how php works but the following script is supposed to -

Get the no of tracks required for a season,
Display a list of tracks from which the user selects the ones to include in the season,
Then display only the tracks selected for the user to add a round number and the date it should be held,
(All the above happens)
This should then be put into a mysql table
(This doesn't happen)

The print statements (put in to see whats happening) in case"Add Rounds": segment show

print_a(NULL)0

Wheream I doing wrong ?

Code: Select all

<?php
include "debuglib.php";


switch($_POST[action])

{
case "Select Circuits":
$selects = sizeof($rounds);

   if ($selects == $norounds)
     {

      $i= 0;

	$display_block = "<h1>Enter Round Number, Date for each circuit</h1>";

      while ($i < $selects)
        {

	      	
         $get_list3 = "SELECT track_name from GPL_circuits WHERE id = $rounds[$i]";
         $get_list3_res = mysql_query($get_list3) or die("Didn't get the track name");
         $track_name = mysql_result($get_list3_res,0,track_name);
	 
	   $display_block .="
	   <form method="post" action="$_SERVER[PHP_SELF]">";
         $display_block .="Round Number
	   <INPUT type="text" name="$rounds[$i]['rid']" size=2>";
	   $display_block .="to be held at "$track_name" on 
         <INPUT type="text" name="$rounds[$i]['date']" size=10><br>";

         $i++;
        }

	$display_block .="
	<p><INPUT type="submit" name="action" value="Add Rounds"></p>
      </form>";
     }
   else
     {
      print"Incorrect Number of tracks selected";
     }

      break;


case "Add Rounds":
      $i= 0;
            print_a($rounds);
            print"$selects";
            print"$i"; 

      while ($i < $selects)
           {


            $make_link = "INSERT INTO GPL_schedule VALUES ('$cursid', '$sel_divid', '$rounds[$i], '$rounds[$i]['rid'], '$sel_tid[$i]['date']')";
            $make_link_res = mysql_query($make_link) or die("Failed to add");

            $i++;
           }
      break;

default:

   $get_list = "select notracks from GPL_season WHERE id = $sel_sid";
   $get_list_res = mysql_query($get_list) or die("Failed to get no of tracks");
   $notracks = mysql_result($get_list_res,0,notracks);

   $get_list2 = "select id, track_name from GPL_circuits";
   $get_list2_res = mysql_query($get_list2) or die("Failed to load track names");

   $selects = 0;

   $display_block = "<h1>Select Circuits for Schedule</h1>";

   $display_block .= "
   <form method="post" action="$_SERVER[PHP_SELF]">
   <input type="hidden" name="sel_divid" value="$sel_divid">";

   while ($trackrecs = mysql_fetch_array($get_list2_res))
     {
      $id = $trackrecs[id];
      $track_name = stripslashes($trackrecs[track_name]);
      $display_block .= "
      <input type="hidden" name="norounds" value="$notracks">
      <input type="checkbox" name="rounds[]" value="$id">$track_name";
     }

   $display_block .= "
   <p><INPUT type="submit" name="action" value="Select Circuits"></p>
   </form>";
   break;
}


?>

Posted: Tue Jul 27, 2004 9:18 am
by gurjit
your insert just has values:

Code: Select all

<?php
$make_link = "INSERT INTO GPL_schedule VALUES ('$cursid', '$sel_divid', '$rounds[$i], '$rounds[$i]['rid'], '$sel_tid[$i]['date']')"; 

?>
SOULD INCLUDE THE FIELD NAMES IN THE DATABSE LIKE:

Code: Select all

<?php
$make_link = "INSERT INTO GPL_schedule (cursid,sel_divid,rounds1,rounds2,sel_tid,date) VALUES ('$cursid', '$sel_divid', '$rounds[$i], '$rounds[$i]['rid'], '$sel_tid[$i]['date']')"; 

?>

Posted: Tue Jul 27, 2004 9:19 am
by pickle
$rounds isn't defined anywhere

Posted: Tue Jul 27, 2004 10:53 am
by feyd
I've never heard of a print_a function. Btw, quote your named array indices, i.e. action, notracks...

Posted: Tue Jul 27, 2004 11:27 am
by fccolon
Thanks for the replies guys.....I'll try them out and let you know how I get on, tho on the one or two other INSERTS I've done I haven't had to include the field names - or is that more for debugging purposes.

the print_a function comes from the included dubuglib.php script which I got from

http://www.atomar.de/public/code/debugl ... b.demo.php

one of the things it does is to print an array out in pretty colours etc - thought it might be easier for me to follow

Posted: Tue Jul 27, 2004 11:31 am
by feyd
if you were inserting less than full row details, you'd have to have column names, but if you're inserting the entire rows values, you don't absolutely require them..

Posted: Wed Jul 28, 2004 3:40 am
by fccolon
Haven't had a chance to retest yet, but I have another question regarding arrays - in this code snip I'm setting up checkboxes for the admin to choose a number of circuits from a list, currently only storing the record id of the selected circuit,

Code: Select all

while ($trackrecs = mysql_fetch_array($get_list2_res)) 
     { 
      $id = $trackrecs[id]; 
      $track_name = stripslashes($trackrecs[track_name]); 
      $display_block .= " 
      <input type="hidden" name="norounds" value="$notracks"> 
      <input type="checkbox" name="rounds[]" value="$id">$track_name"; 
     }
Can I also store the circuit name ($track_name) in rounds[] , like this ?

Code: Select all

while ($trackrecs = mysql_fetch_array($get_list2_res)) 
     { 
      $id = $trackrecs[id]; 
      $track_name = stripslashes($trackrecs[track_name]); 
      $display_block .= " 
      <input type="hidden" name="norounds" value="$notracks"> 
      <input type="checkbox" name="rounds[]" value="$id","$track_name">$track_name"; 
     }

Posted: Wed Jul 28, 2004 5:38 am
by feyd
you could store them like this:

Code: Select all

$display_block .= '
<input type="hidden" name="norounds[' . $id . ']" value="' . $notracks . '">
<input type="checkbox" name="rounds[' . $id . ']" value="' . $track_name . '">' . $track_name;
then when submitted:

Code: Select all

foreach($_POST['rounds'] as $id => $track_name)
{
//....
}
that's assuming at least 1 was selected...

Posted: Wed Jul 28, 2004 8:59 am
by fccolon
pickle wrote:$rounds isn't defined anywhere
I do get values displayed from $rounds if I print_r (or print_a) it within

Code: Select all

case"Select Circuits":
{
..
..
}
just not in

Code: Select all

case"Add Rounds":
{
..
..
}
the switch() code is being executed in the sequence

default
Select Circuits
Add Records

Posted: Thu Jul 29, 2004 9:45 am
by pickle
But the "Select Circuits" case never gets touched if $_POST['action'] equals "Add Rounds".

Posted: Thu Jul 29, 2004 10:46 am
by fccolon
Which confirms how little I know about coding, and my script is dissappearing up it's own backside :)

This script is an include on another -

In the first script I select a season from a list already in the db, then select a division. A record is put into a table to link these two together. Then the script above is run.
Initially I get a list of Circuits (provided by the default case, from which I checkbox the ones I want to include in the previously selected season, then I hit the 'Select Circuits' button.
I now get a series of lines - or did til I tried changing code to fix this problem and made it worse ;) - which are

Round Number (empty box) to be held at Lime Rock Park on (empty box)

for each of the circuits selected. Once I've filled in the boxes I hit the button 'Add Rounds' and ............ nothing.

As I said, I've only just started learning php/html/mysql, so am trying something that's way over my ability.......but I'll get there in the end.....or the pc will get both barrells within a week.
I wanted to find out about web design/programming and I happen to be in an Online racing league, so I thought, combine the two and create a site to hold the results etc.

Thanks for taking the time to look at this, much appreciated :)