Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello Everyone..

 

Im a programer in php with little experience.

I have a form with 3 pages. 1st page display categories which come from category table. then user can select 1 or upto 3 category there. Then 2nd page display that selected categories and need to display relevant subject to that categories. In second page I use radio button for selected categories to keep a one category as a main category. each categories' subjects display with check boxes. In there too, user can select 1 or 3 more subject in to a particular category. In third form page, that my problem was encounter, there I need to print out selected category and their subjects in a table.

 

first two pages are no problem for me and third form I have a problem in how to get categories and their subjects and they print as apiece in the page. like this

 

category1 | subject1, subject2, subject3

category2 | subject1, subject2, subject3, subject and so forth.

 

This same to my first form page

 

$q = 'SELECT * FROM category ORDER BY category_id';
$r = mysqli_query( $dbc, $q);
$c = 0;
$i = 0;
echo '<form action="subjects.php" method="post">';
echo '<table class="form_table" ><tr>';
while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC  )){
 // if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row: 
 if( ($c % 2) == 0 && $c != 0){
  echo "</tr><tr>";
 }
 echo '<td width="50%"><input type="checkbox" name="category[]"  value="' . $row['category_id'] . '" />  ' . $row['category_name'] . '</td>';
$c++;
} // while..
 // in case you need to fill a last empty cell:
 if ( ( $i % 2 ) != 0 ){
 // str_repeat() will be handy when you want more than 2 columns
   echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
 }
echo "</tr></table>";
?>

 

 

In second page 'subjects.php' I use

 

if ( isset($_POST['submitted1']) && isset($_POST['category']) && sizeof( $_POST['category']) == 3 ) {

 $_SESSION['category'] = $_POST['category'];

 print_r ( $_SESSION['category']);

 $q = 'SELECT * FROM category ORDER BY category_id';
 $r = mysqli_query( $dbc, $q);

 while($_SESSION = mysqli_fetch_array( $r, MYSQLI_ASSOC  )){

  foreach( $_POST['category'] as $value ) {

   if ( $value == $_SESSION['category_id']) {

  $q = "SELECT category_subject.category_id, category_subject.subject_id, subjects
    FROM category_subject
    INNER JOIN category ON category_subject.category_id = category.category_id
    INNER JOIN subject ON category_subject.subject_id = subject.subject_id
    WHERE category_subject.category_id = {$_SESSION['category_id']}";

  $r1 = mysqli_query( $dbc, $q);
  $c = $i = 0;

  echo '<table class="form_table" ><tr>';
  while($_SESSION = mysqli_fetch_array( $r1, MYSQLI_ASSOC  )){

   // if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row: 
   if( ($c % 2) == 0 && $c != 0){
    echo "</tr><tr>";
   }
   echo '<td width="50%"><input type="checkbox" name="subject[]"  value="' . $_SESSION['subject_id'] . '" />  ' . $_SESSION['subjects'] . '</td>';

  $c++;
  } // while..
   // in case you need to fill a last empty cell:
   if ( ( $i % 2 ) != 0 ){

   // str_repeat() will be handy when you want more than 2 columns
	 echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
   }
  echo "</tr></table>";	
   }
  }  
 }

}

 

 

Those two pages working properly.

 

In third page that my problem page I tried something like this , but its not working.

value come from subject.php page.

 

if ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) && (isset( $_SESSION['subjects'])) && sizeof( $_SESSION['category']) == 1) {

 //$_SESSION['category'] = $_POST['category'];

 print_r ( $_SESSION['category']);

 echo 'good';


} elseif ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) &&
 (isset( $_POST['main_category'] ))) {

 print_r ( $_SESSION );


 echo 'very good';

}

 

Note: I have started the sessions properly in every pages.

 

So can any body tell me where is my mistake and help me fix this problem.

 

any help greatly appreciated.

 

thank you.

Share this post


Link to post
Share on other sites

Lot of logic here to display tables. This is not tabular data, and it's really making your script more difficult to understand than needed. Use divs and format them with CSS instead.

 

Now back to your issue:

I would build a multidimensional session array along the pages that map subjects to categories. This is an illustration in pseudo code.

 

$_SESSION['category_table_key'] = array(
"subject_table_key" => array(), // Array is all info from DB query ( aka the row = mysqli_fetch_array() call)
"subject_table_key" => array(),
"subject_table_key" => array(),
);

 

 

This is a (probably) working second page. You need to add output and such, but session array should be built correctly.

 

if ( isset($_POST['submitted1']) && isset($_POST['category']) && count( $_POST['category']) == 3 )
{

// Start building main query
$start = "SELECT category_subject.category_id, category_subject.subject_id, subjects
FROM category_subject
INNER JOIN category ON category_subject.category_id = category.category_id
INNER JOIN subject ON category_subject.subject_id = subject.subject_id
WHERE "; // We will join where clause later

// Build where clause array used in query
foreach ( $_POST['category'] as $key => $value )
{
	$subject_category[] = "category_subject.category_id = {$key}";
}

// Create where clause for all selected subjects (Notice implode with OR. Will give us all subjects for selected categories)
$where = implode(" OR ", $subject_category);

// Concatenate final query
$query = $start.$where;

  // Run query
  $result = mysqli_query( $dbc, $query );

// Add to session
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ))
{
	// Here we have it. Add all subjects to the correct session key ($row['category_id']
	$_SESSION[$row['category_id']] = $row;

	// You can specify output here too...
}

  // Check session array. it will hold all info about courses in selected categories.
echo '<pre>' , print_r($_POST), '</pre>';

}

 

Third page is now very simple

 

foreach ( $_POST as $category => $courses)
{
echo $category.' has these courses:';
foreach ( $courses as $course )
{
	echo $course.' ':
}
}

  • Upvote 1

Share this post


Link to post
Share on other sites

Antonio,

Thanks for your answer. I've been trying to do it according to your coding. still I am in the problem in 3rd form. I'm not sure where I want to go. Sorry.

 

I am some confusing in multidimensional array.

 

1st and 2nd form working for me. but 3rd one is confusing. I am trying to get values to multidimensional array but I cant.

 

my second form also dynamically generating according to 1st form categories which user selected. like this

 

category1

subject1

subject2

subject3

 

category2

subject2

sbuject3

subject4 , like wise.

 

in there, all subjects display relevant to that selected category. subjects also come from my subject table.

 

this is the code I used to generate my second form

 

if ( isset($_POST['submitted1']) && isset($_POST['category']) && ( sizeof( $_POST['category']) == 2 || sizeof( $_POST['category']) == 3 )) {

  foreach( $_POST['category'] as $value ) {

  $q = "SELECT * FROM category WHERE category_id = $value ORDER BY category_id" ;
  $r = mysqli_query( $dbc, $q);

  $row = mysqli_fetch_array ( $r, MYSQLI_ASSOC);   

   echo '<input type="radio" name="main_category[]" class="radio"  value="' . $row['category_id'] . '" />';  
   echo '<label for="" class="radio">  ' . $row['category_name'] . ' <strong>: Mark this category as my "Main Category"</strong></label><br />';

   $q = "SELECT category_subject.category_id, category_subject.subject_id, subjects
	 FROM category_subject
	 INNER JOIN category ON category_subject.category_id = category.category_id
	 INNER JOIN subject ON category_subject.subject_id = subject.subject_id
	 WHERE category_subject.category_id = {$row['category_id']}";
   $result = mysqli_query( $dbc, $q);
   $c = $i = 0;

   echo '<table class="form_table" ><tr>';
    while($row = mysqli_fetch_array( $result, MYSQLI_ASSOC  )){

	 // if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row: 
	 if( ($c % 2) == 0 && $c != 0){
	  echo "</tr><tr>";
	 }
	 echo '<td width="50%"><input type="checkbox" name="subject[]"  value="' . $row['subject_id'] . '" />  ' . $row['subjects'] . '</td>' . "\n";
	 $c++;
    } // while..
	 // in case you need to fill a last empty cell:
	 if ( ( $i % 2 ) != 0 ){
	 // str_repeat() will be handy when you want more than 2 columns
	   echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
	 }
    echo "</tr></table>";	   
  }   


 }

 

Its work properly. Now I need to get category and only their selected subject to my 3rd form to display those values. actually I need to get their value as apiece. that mean category1 and their selected subject, category2 and their selected subjects and so on. I think solution should have a multidimensional array. I am very confusing there.

 

Thanks

Share this post


Link to post
Share on other sites

thara, I'll be honest, I didn't even try to read through all your code. It's all over the place.

Instead, I will offer a solution by writing a basic script (that cuts out all the unnecessary junk) that I *think* demonstrates what you want to do. Please feel free to comment on it.

 

page1.php

 

<!DOCTYPE html>

 

<html>

 

<body>

 

<form action="page2.php" method="post">

 

<input type="checkbox" name="subject[]" value="subject1">Subject 1<br>

 

<input type="checkbox" name="subject[]" value="subject2">Subject 2<br>

 

<input type="checkbox" name="subject[]" value="subject3">Subject 3<br>

 

<input type="submit" value="Submit">

 

</form>

 

</body>

 

</html>

 

In this first script, there are three checkboxes, and a submit button. This will submit to page2.php.

 

page2.php

 

<?php

 

if (isset($_POST['subject'])) {

 

echo '<form action="page3.php method="post">';

 

foreach ($_POST['subject'] as $value) {

 

/* Make DB request for each of the values in $value, and echo the results as necessary. Also, echo hidden input elements so you can send the data from the first page to the third page as well. For example: */

 

$q = "SELECT data FROM subjects_table WHERE subject = '$value'";

 

$r = mysqli_query($dbc, $q);

 

if ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {

 

echo '<input type="radio" name="category[]" value="' . $row[0] . '"><br>

 

<input type="hidden" name="subject[]" value="' . $value . '"><br>';

 

} else {

 

// DB error with the value stored in $value. Do something to handle this.

 

}

 

}

 

echo '</form>';

 

} else {

 

// No checkboxes were selected. Return the user to page1.php.

 

}

 

?>

 

The key with page2.php is to create the necessary radio buttons based on the info sent from page1.php but also to use hidden input elements to pass the info gotten from page1.php to page3.php.

 

page3.php

 

<?php

 

if ((isset($_POST['subject'])) && (isset($_POST['category']))) {

 

// Display all the data.

 

echo 'Main categories:<br>';

 

foreach ($_POST['category'] as $value) {

 

echo $value . '<br>';

 

}

 

echo 'Subjects:<br>';

 

foreach ($_POST['subject'] as $value) {

 

echo $value . '<br>';

 

}

 

} else {

 

// Data missing somewhere. Handle it.

 

}

 

?>

 

Of course, you can organize the data however you want, but I think the key is to somehow pass the data from page1.php all the way to page3.php. Naturally, you will want to include all sorts of checks along the way, but the above code is a basic demonstration. To be honest, I'd probably go with Antonio's method of using the $_SESSION superglobal, but since you seemed confused by all that array manipulation, I thought I'd post this method as well.

 

Good luck, and sorry if I missed the mark, but it's hard to understand what you want.

 

Also, sorry for the sloppy post and lack of testing of the scripts, but stuck on IE6 at work.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks HartleySan for your answer. I tried to do according to your explanation. But still I could not to get my expecting result.

page1.php page ok it display every category from my database.

 

 echo '<td width="50%"><input type="checkbox" name="category[]"  value="' . $row['category_id'] . '" />  ' . $row['category_name'] . '</td>';

 

In my second page I need to display selected categories from 1st page and relevant subjects to that categories.

eg:

cat1 | sub1, sub2, sub3

cat2 | sub1, sub2, sub3,

cat3 | sub1, sub4, sub5 and so on.

 

every category has some subjects and all of those subjects should be display in my second page as category wise. subject also come from my subject table. category_subject table also available to make the relationship between every category and subject. In second page also working as I am expecting. above I have posted my second page coding.

 

But my problem is 3rd page.

 

In second page users can select 3 more subjects to 1 category. assume there is 3 category users can select 9 subjects. so my problem is how I bring those category and their subjects from 2nd page to my 3rd page.

 

actually I am very confusing here and I try to do this for days. unfortunately still I cant get my expecting results.

 

I waiting for any comments and those are greatly appreciated.

 

thank you.

Share this post


Link to post
Share on other sites

If you could take a look at the 4th edition of PHP and MySQL for Dynamic Web Sites Pg.177 there is a messages table there which is constituting of message_id and parent_id. Well the structure of a category table would be a replicate of this and would only require basic SQL queries to draw out subcategories etc. I also don't quite understand why you have three form pages. Why don't you just make this a one page php script and use the $_POST, $_SESSION arrays to pass the information into it at the top of the PHP script before it actually renders the semantic HTML. You could look at this like a category-view.php PHP script. And there is pretty much no difference between a sticky form and a Selected form element.

 

Do you have other website examples of what you are trying to do, in the case here that we are missing the exact point of what it is exactly that you are trying to accomplish.

Share this post


Link to post
Share on other sites

I agree with Edward, thara. Using the $_SESSION array is the easiest solution, and I believe Antonio already provided such a solution, but the array structures seemed to confuse you, which is why I provided a $_POST solution as well.

 

Beyond all the advice we've already given you, we can't help you too much more without more details.

 

thara, tell you what, I'll make a deal: Give us an exact description of all the possible categories and subcategories that can be used on your form, and we'll help you code it, one step at a time. Unfortunately, without specific data, it's hard for us to tailor examples to what you need.

 

I get the feeling that a lot of the problems you are having are in the details and syntax of your code.

Share this post


Link to post
Share on other sites

I had a look at that website, i wasn't really impressed by the registration process, it has its way of over complicating matters with clustered data which is reflecting in your code. On the registration form there are three criteria Subjects, Classes and Experience level, these all need to be split up into separate parts. I would suggest you redesign this form so that each new member is asked what subject they can tutor, then ask them for the level of experience which they can teach it at (MBA for example). There may be someone that can teach perhaps 10 subjects so limiting them to 3 or 4 on the the first form would be wrong. Once a member has entered one subject which they can teach ask them if they have another subject they can tutor for, this way populating the database dynamically. In my opinion they have a sloppy form, it would be best for you to redesign the whole registration process and you will have a better functioning website. Don't always copy what you see online because even though they have a web site online it doesn't mean its standard practice.

  • Upvote 1

Share this post


Link to post
Share on other sites

Let me add something else to this, it would be best to take the personal details for registration first of all then have a user based log in system. Where by when a user was logged into the website they could add subjects they tutor within their user account.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...