Where's my logic going wrong ?

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
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Where's my logic going wrong ?

Post 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;
}


?>
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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']')"; 

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

$rounds isn't defined anywhere
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've never heard of a print_a function. Btw, quote your named array indices, i.e. action, notracks...
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post 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"; 
     }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

But the "Select Circuits" case never gets touched if $_POST['action'] equals "Add Rounds".
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fccolon
Forum Newbie
Posts: 10
Joined: Mon Jul 19, 2004 6:13 am
Location: UK

Post 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 :)
Post Reply