Solved

Requesting ID's from database and print results

Posted on 2007-12-06
30
196 Views
Last Modified: 2013-12-12
I have this page  - http://newspapersofamerica.com/new/?p=specialty - and I have been trying to work with this most of the morning and am stumped. I know how to do both of the methods separate but am confused how to put both together.

On that page you will select a state, then it will show all of the of the papers that belong to that state.
I want to add one more step. I have the a table called PAPER_CATEGORIES and I want to be able to select ALL papers from category X, then states, then all papers from that state and category.

http://newspapersofamerica.com/new/?p=specialty - go to the "Specialty" and on mouse over are the categories. So the link will be something like say:

index.php?p=specialty&cat=3 on that page will show the states then click that will show all states with papers belonging to CAT 3.



Would I use:

if($_REQUEST["id"]){

For the request part. My code is below for my specialty page.

This has been bugging me! Thanks so much. I will post any changes i have. I am going to try to figure it out.




<?php

if(isset($_REQUEST["state"]) && $_REQUEST["state"]!=""){

        $id     = $_REQUEST['state'];

        $sql    = "SELECT * FROM papers WHERE state_id='$id' AND cat_id=2";

        $query  = mysql_query($sql) or die(mysql_error());

        $state  = $_GET["state_name"];

        

        $state_name_query = "SELECT state_name FROM states WHERE state_id=$id";

        $get_state_name = mysql_query($state_name_query) or die(mysql_error());

        list($state)    = mysql_fetch_row($get_state_name);

        print "

        <h2>$state</h2>

        <table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

                <thead> 

                        <tr> 

                        <th>Paper Name</th> 

                        <th>City</th> 

                        <th>Circulation</th> 

                        <th>Price</th>

                        <th></th>

                        <th></th>

                        </tr> 

                </thead>

                <tbody>

              ";

        while($row = mysql_fetch_array($query)){

        print "<tr>";

                print "<td>".$row["name"]."</td>\n"; 

                print "<td>".$row["city"]."</td>\n"; 

                print "<td>".$row["circulation"]."</td>\n";

                print "<td>$".$row["price"]."</td>\n";

                print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=".$row["id"]."\" class=\"jTip\" id=\"desc_".$row["id"]."\" rel=\"description.php?id=".$row["id"]."&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

                print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

        print "</tr>\n";

        }

        print "</tbody>\n";

        print "</table>\n";

 

}else 

{

?>

<h2>Specialty Papers</h2>

<h3>Pick a state below to view papers.</h3>

        <table id="state">

<?php

error_reporting(E_ALL);

$query = mysql_query("SELECT state_name,state_id FROM states");

$i=1;

while($row1 = mysql_fetch_array($query)){

    if($i%2){

        echo '<tr><td width="300" class="st"><a href="?p=speciality&state='.$row1["state_id"].'">'.$row1["state_name"].'</a></td>';

    }

    else{

        echo '<td width="300" class="st"><a href="?p=speciality&state='.$row1["state_id"].'">'.$row1["state_name"].'</a></td></tr>';

    }

    $i++;

}

if(mysql_num_rows($query)%2) {

    print "<td>&nbsp;</td></tr>";

}

?>

        </table>

<?php

        //echo "Error has occured"; <-- what is this for?

}

?>

Open in new window

0
Comment
  • 14
  • 14
  • 2
30 Comments
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20422786
I am looking at it some more and the main part I am stuck is getting to select the CAT then display the states, then go to the papers.

You would use REQUEST and reuqest the info and sent it through the URL
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20422887
i guess in your url you will need another variable called cat_id or something similar, and then check if cat_id is set, if it is set then display a list of categories (this is the second step) and the user can click on a particular cat_id and that url will contain botht he cat_id and the state_id, which will narrow the results down to what you want (third step) this is the general concept.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423058
Thats what I am thinking as well, for my URL to display papers in list i have this:

?p=specialty&cat_id=4&state=5

Does this look correct:

if(isset($_REQUEST["state"]) && $_REQUEST["state"]!=""){
        $cat_id = $_REQUEST['cat_id'];
        $id     = $_REQUEST['state'];
        $sql    = "SELECT * FROM papers WHERE state_id='$id' AND cat_id='$cat_id'";
        $query  = mysql_query($sql) or die(mysql_error());

for the URL above. I know I am going to need to alter the if, but not sure how?

Here is my whole code:








<?php

if(isset($_REQUEST["state"]) && $_REQUEST["state"]!=""){

        $cat_id = $_REQUEST['cat_id'];

        $id     = $_REQUEST['state'];

        $sql    = "SELECT * FROM papers WHERE state_id='$id' AND cat_id='$cat_id'";

        $query  = mysql_query($sql) or die(mysql_error());

        

        // used to get state name and print out

        $state            = $_GET["state_name"];

        $state_name_query = "SELECT state_name FROM states WHERE state_id=$id";

        $get_state_name   = mysql_query($state_name_query) or die(mysql_error());

        list($state)      s= mysql_fetch_row($get_state_name);

        // END

        print "

        <h2>$state</h2>

        <table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

                <thead> 

                        <tr> 

                        <th>Paper Name</th> 

                        <th>City</th> 

                        <th>Circulation</th> 

                        <th>Price</th>

                        <th></th>

                        <th></th>

                        </tr> 

                </thead>

                <tbody>

              ";

        while($row = mysql_fetch_array($query)){

        print "<tr>";

                print "<td>".$row["name"]."</td>\n"; 

                print "<td>".$row["city"]."</td>\n"; 

                print "<td>".$row["circulation"]."</td>\n";

                print "<td>$".$row["price"]."</td>\n";

                print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=".$row["id"]."\" class=\"jTip\" id=\"desc_".$row["id"]."\" rel=\"description.php?id=".$row["id"]."&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

                print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

        print "</tr>\n";

        }

        print "</tbody>\n";

        print "</table>\n";

 

}else 

{

?>

<h2>Specialty Papers</h2>

<h3>Pick a state below to view papers.</h3>

        <table id="state">

<?php

error_reporting(E_ALL);

$query = mysql_query("SELECT state_name,state_id FROM states");

$i=1;

while($row1 = mysql_fetch_array($query)){

    if($i%2){

        echo '<tr><td width="300" class="st"><a href="?p=speciality&state='.$row1["state_id"].'">'.$row1["state_name"].'</a></td>';

    }

    else{

        echo '<td width="300" class="st"><a href="?p=speciality&state='.$row1["state_id"].'">'.$row1["state_name"].'</a></td></tr>';

    }

    $i++;

}

if(mysql_num_rows($query)%2) {

    print "<td>&nbsp;</td></tr>";

}

?>

        </table>

<?php

        //echo "Error has occured"; <-- what is this for?

}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423096
When I do this:

?p=specialty&cat_id=4&state=5

I get the only result and the criteria matches up, but when I only do:

?p=specialty&cat_id=4

I get the states but the papers doesnt work after I click it.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20423141
hi ryan,

you will need an additional if statement, so something like this:

if(State_id is set)
{
// so display all categories
}
else if(State_id is set and category_id is set)
{
  // so display all papers related to the category and state_id
}
else
{
// initial condition, display all states!
}
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423183
Alrighty..I don't think I am going to need that first step am I right?

I am going to have the categories from links on the header page so I dont think I need it right? so would it just be

IF
ELSE

So the first would be:

if(State_id is set and category_id is set) which is this right?

if(isset($_REQUEST["state"]) && $_REQUEST["state"]!=""){


How would I add in the cat_id?

&& $_REQUEST["cat_id"]
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423243
Hi,

I have been messing with this. Is this what you are looking for?
<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category_name FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst['state_name'] . " - " . $rst2['category_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query2);

							if($result3){

								//output info

								while($rst3 = mysql_fetch_array($result3)){

									print "<tr>";

									print "<td>" . $rst3['name'] . "</td>\n"; 

									print "<td>" . $rst3['city'] ."</td>\n"; 

									print "<td>" . $rst3['circulation'] . "</td>\n";

									print "<td>$" . number_format($rst3['price'], 2) ."</td>\n";

									print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

									print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

									print "</tr>\n";

								}

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['state'])){	

				//get value

				$state = trim($_GET['state']);

										

				//get states

				$query = "SELECT state_name, state_id FROM states WHERE state_id='$state' LIMIT 1";

				$result = mysql_query($query);

				if($result){

					if(mysql_num_rows($result)>0){

						//output info

						$rst = mysql_fetch_array($result);

						print '<h2>' . $rst['state_name'] . ' - Categories</h2>

						   <h3>Pick a category below to view papers.</h3>

								<table id="categories">';

								

						//get categories 

						$query2 = "SELECT category_name FROM paper_categories ORDER BY category_name ASC";

						$result2 = mysql_query($query2);

						if($result2){

							//output info

							$count = 0;

							while($rst2 = mysql_fetch_array($result2)){

								if($count%2){

									print '<tr><td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '&cat=' . $rst['cat_id'] . '">' . $rst2['category_name'] . '</a></td>';

								}else{

									print '<td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '&cat=' . $rst['cat_id'] . '">' . $rst2['category_name'] . '</a></td></tr>';

								}

								$count++;

							}

							if(mysql_num_rows($result2)%2){

								print '<td>&nbsp;</td></tr>';

							}

						}

						print '</table>';

					}else{

						//error

						print "state not found";

					}

				}else{

					print 'error';

				}

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423304
Yeah...from looking at my code it is. I want to say look at all papers in CATEGORY 4 so I will do

index.php?p=specialty&cat=4

That shows all states, but I click California and that state meets the criteria..I have a row that is in that category and in that state and it doesnt show.

Any suggestions?
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423329
To show all the states go to:

index.php?p=specialty

The from there you should see the states. Clicking on one of them should show the categories.

Clicking on a category should show the papers?
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423350
Oops.. There was some errors. Try:
<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category_name FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst['state_name'] . " - " . $rst2['category_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query2);

							if($result3){

								//output info

								while($rst3 = mysql_fetch_array($result3)){

									print "<tr>";

									print "<td>" . $rst3['name'] . "</td>\n"; 

									print "<td>" . $rst3['city'] ."</td>\n"; 

									print "<td>" . $rst3['circulation'] . "</td>\n";

									print "<td>$" . number_format($rst3['price'], 2) ."</td>\n";

									print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

									print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

									print "</tr>\n";

								}

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['state'])){	

				//get value

				$state = trim($_GET['state']);

										

				//get states

				$query = "SELECT state_name, state_id FROM states WHERE state_id='$state' LIMIT 1";

				$result = mysql_query($query);

				if($result){

					if(mysql_num_rows($result)>0){

						//output info

						$rst = mysql_fetch_array($result);

						print '<h2>' . $rst['state_name'] . ' - Categories</h2>

						   <h3>Pick a category below to view papers.</h3>

								<table id="categories">';

								

						//get categories 

						$query2 = "SELECT cat_id, category_name FROM paper_categories ORDER BY category_name ASC";

						$result2 = mysql_query($query2);

						if($result2){

							//output info

							$count = 0;

							while($rst2 = mysql_fetch_array($result2)){

								if($count%2){

									print '<tr><td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '&cat=' . $rst2['cat_id'] . '">' . $rst2['category_name'] . '</a></td>';

								}else{

									print '<td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '&cat=' . $rst2['cat_id'] . '">' . $rst2['category_name'] . '</a></td></tr>';

								}

								$count++;

							}

							if(mysql_num_rows($result2)%2){

								print '<td>&nbsp;</td></tr>';

							}

						}

						print '</table>';

					}else{

						//error

						print "state not found";

					}

				}else{

					print 'error';

				}

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423357
After going to the main page and clicking one of the states I dont see the categories..I only have "California" as having data in there and as cat_id of 4. If that helps.

From the nav I am going to have my links as:

?p=specialty&cat=4 - weekly
...
..

And write it out, then they click one and it shows states ,then papers.
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423373
So the order is Category -> States -> Papers?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423379
Correct! i tried your new code and I didnt see anything..I get the method now after reviewing your code! i had a similar idea, but yours is much more detailed.

Thanks,
Ryan
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423418
Ok, Can I just check some things with you. Is the table for your categories called: paper_categories and in there are the fields cat_id and category_name correct?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423430
That is correct.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:nacker2000
ID: 20423493
If the info I asked you above is correct then I think this code is right now...
<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category_name FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst2['category_name'] . " - " . $rst['state_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query2);

							if($result3){

								//output info

								while($rst3 = mysql_fetch_array($result3)){

									print "<tr>";

									print "<td>" . $rst3['name'] . "</td>\n"; 

									print "<td>" . $rst3['city'] ."</td>\n"; 

									print "<td>" . $rst3['circulation'] . "</td>\n";

									print "<td>$" . number_format($rst3['price'], 2) ."</td>\n";

									print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

									print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

									print "</tr>\n";

								}

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['cat'])){

				//get value

				$cat = trim($_GET['cat']);	

				

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a category below to view papers.</h3>

							<table id="category">';

				//get categories 

				$query = "SELECT cat_id, category_name FROM paper_categories ORDER BY category_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423541
Oops.. use this instead, soz :)
<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category_name FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst2['category_name'] . " - " . $rst['state_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query2);

							if($result3){

								//output info

								while($rst3 = mysql_fetch_array($result3)){

									print "<tr>";

									print "<td>" . $rst3['name'] . "</td>\n"; 

									print "<td>" . $rst3['city'] ."</td>\n"; 

									print "<td>" . $rst3['circulation'] . "</td>\n";

									print "<td>$" . number_format($rst3['price'], 2) ."</td>\n";

									print "<td class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

									print "<td align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

									print "</tr>\n";

								}

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['cat'])){

				//get value

				$cat = trim($_GET['cat']);	

				

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a category below to view papers.</h3>

							<table id="category">';

				//get categories 

				$query = "SELECT cat_id, category_name FROM paper_categories ORDER BY category_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423566
On line 88 in the coe above there is a double && where is says:

<a href="?p=speciality&cat=' . $cat . '&&state=' . $rst['state_id'] . '">

That should be:

<a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20423590
Nice! Thank you very much, i have a couple of questions.

If go to click a state, it doesnt show all of the paper info, in the table. i get blank data and i made sure i have a row there..any suggestions?

On this part: index.php?p=specialty&cat=4&state=2

Also..anyway to fix how the right col is offset by one it seems? I looked and not sure why it is doing that?
Thanks so much. I really appreciate it.

I got those changes..thanks! Little typos.

Thanks again for your help!

0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20423655
As I can't see the page its hard to see what you mean about the right column being offset, but it may be due to the table being 100% width and no widths being defined for the columns. Try this (yet more code, lol)
<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category_name FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst2['category_name'] . " - " . $rst['state_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query3);

							if($result3){

								//output info

								while($rst3 = mysql_fetch_array($result3)){

									print "<tr>";

									print "<td width=\"25%\">" . $rst3['name'] . "</td>\n"; 

									print "<td width=\"15%\">" . $rst3['city'] ."</td>\n"; 

									print "<td width=\"15%\">" . $rst3['circulation'] . "</td>\n";

									print "<td width=\"15%\">$" . number_format($rst3['price'], 2) ."</td>\n";

									print "<td width=\"15%\" class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

									print "<td width=\"15%\" align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

									print "</tr>\n";

								}

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['cat'])){

				//get value

				$cat = trim($_GET['cat']);	

				

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a category below to view papers.</h3>

							<table id="category">';

				//get categories 

				$query = "SELECT cat_id, category_name FROM paper_categories ORDER BY category_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 0;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424072
See the right col here how it is one lower -  http://newspapersofamerica.com/new/?p=specialty&cat=4

My field as category not category_name..sorry I fixed those up.

http://newspapersofamerica.com/new/?p=specialty - I think it works..check me. i dont have any rows. but no errors no blank page.
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20424581
Did you try the latest code above? When it gets the info for the pages, line 39 above was wrong.

It was:
$result3 = mysql_query($query2);

but this should of been:
$result3 = mysql_query($query3);

If you have implemented this change and it still doesn't work then try the code below. I have updated it so that it uses category instead of category_name and I think I may have fixed the offset. Plus I have put in some checks to output if its failing to get the papers.


<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst2['category'] . " - " . $rst['state_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query3);

							if($result3){

								if(mysql_num_rows($result3)>0){

									//output info

									while($rst3 = mysql_fetch_array($result3)){

										print "<tr>";

										print "<td width=\"25%\">" . $rst3['name'] . "</td>\n"; 

										print "<td width=\"15%\">" . $rst3['city'] ."</td>\n"; 

										print "<td width=\"15%\">" . $rst3['circulation'] . "</td>\n";

										print "<td width=\"15%\">$" . number_format($rst3['price'], 2) ."</td>\n";

										print "<td width=\"15%\" class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

										print "<td width=\"15%\" align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

										print "</tr>\n";

									}

								}else{

									print "no pages found";

								}

							}else{

								print "failed to get pages";

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['cat'])){

				//get value

				$cat = trim($_GET['cat']);	

				

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 1;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a category below to view papers.</h3>

							<table id="category">';

				//get categories 

				$query = "SELECT cat_id, category FROM paper_categories ORDER BY category ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 1;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=speciality&cat=' . $rst['cat_id'] . '">' . $rst['category'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424639
Nice thank you! Just took a look at the code and I like it :) Afterward i want to ask you some quick questions for I get to understand it better. Learn something :p with that code after clicking a category I dont see any states?

Any idea?

Thanks,

rayn
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20424697
On the links in the code the category value is: speciality I think you may have a typo as is you replace it with: specialty it works.

The papers is now showing 'no pages found'. Which means that there are no papers being found that match the query. Are you sure that your papers have a state id and cat id set in the database?
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20424723
Sorry thats not the category value I mean the p value...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20424754
Weird! I was just about to post that and say the typo on the name of the page. Works great now. Thank you for your help.

Couple questions:

        if($_SERVER['REQUEST_METHOD']=='GET'){
                if(isset($_GET['state'])&&isset($_GET['cat'])){

That block of code will execute when there is say ?p=state=X&cat=X correct?

        if(mysql_num_rows($result2)>0){

If there are rows present more than 0..print correct?

                        $state = trim($_GET['state']);
                        $cat   = trim($_GET['cat']);

Trim there doesn't allow any white space correct?

Do you use a tidier for your code? Or good ole hand? Just curious.

Thank you.


0
 
LVL 5

Accepted Solution

by:
nacker2000 earned 500 total points
ID: 20426100
Hi,

Soz for the slow response but I'm in the UK and my last post was at 1am... So is this working now then? It's still saying 'no papers found', but is this because you haven't entered the papers in to the database yet?

Answers
1:
Couple questions:

        if($_SERVER['REQUEST_METHOD']=='GET'){
                if(isset($_GET['state'])&&isset($_GET['cat'])){

That block of code will execute when there is say ?p=state=X&cat=X correct? = YES

2:
        if(mysql_num_rows($result2)>0){

If there are rows present more than 0..print correct? = YES

3:
                        $state = trim($_GET['state']);
                        $cat   = trim($_GET['cat']);

Trim there doesn't allow any white space correct? = Trim removes any spaces from the beginning and end of the string. So "        hello world    " would become "hello world"

4:
Do you use a tidier for your code? Or good ole hand? Just curious. = Nope I just have to have my code tidy. To me having tidy, well formatted code is the most important part of coding. If you can't read through your code then you can't debug it!

Just to keep the code up to date I have corrected my code so that the typo is now fixed:

<?php

 	if($_SERVER['REQUEST_METHOD']=='GET'){

		if(isset($_GET['state'])&&isset($_GET['cat'])){

			//get values

			$state = trim($_GET['state']);

			$cat = trim($_GET['cat']);

			

			//get state info

			$query = "SELECT state_name FROM states WHERE state_id='$state' LIMIT 1";

			$result = mysql_query($query);

			if($result){

				if(mysql_num_rows($result)>0){

					$rst = mysql_fetch_array($result);

					

					//get category info

					$query2 = "SELECT category FROM paper_categories WHERE cat_id='$cat' LIMIT 1";

					$result2 = mysql_query($query2);

					if($result2){

						if(mysql_num_rows($result2)>0){

							$rst2 = mysql_fetch_array($result2);

							//output info

							print "<h2>" . $rst2['category'] . " - " . $rst['state_name'] . "</h2>

									<table id=\"myTable\" width=\"100%\" class=\"tablesorter\">

											<thead> 

													<tr> 

													<th>Paper Name</th> 

													<th>City</th> 

													<th>Circulation</th> 

													<th>Price</th>

													<th></th>

													<th></th>

													</tr> 

											</thead>

											<tbody>

										  ";

				

							//get papers

							$query3 = "SELECT * FROM papers WHERE state_id='$state' AND cat_id='$cat'";

							$result3 = mysql_query($query3);

							if($result3){

								if(mysql_num_rows($result3)>0){

									//output info

									while($rst3 = mysql_fetch_array($result3)){

										print "<tr>";

										print "<td width=\"25%\">" . $rst3['name'] . "</td>\n"; 

										print "<td width=\"15%\">" . $rst3['city'] ."</td>\n"; 

										print "<td width=\"15%\">" . $rst3['circulation'] . "</td>\n";

										print "<td width=\"15%\">$" . number_format($rst3['price'], 2) ."</td>\n";

										print "<td width=\"15%\" class=\"desc\" align=\"center\">\n<span class=formInfo\">\n<a href=\"description.php?id=" . $rst3['id'] . "\" class=\"jTip\" id=\"desc_" . $rst3['id'] . "\" rel=\"description.php?id=" . $rst3['id'] . "&width=250&amp;name=Description%20as%20follow:\"><img src=\"images/newspaper.png\" alt=\"description\" /></a></span></td>\n";

										print "<td width=\"15%\" align=\"center\"><a href=\"#\"><img src=\"images/dollar.png\" alt=\"purchase\" /></a></td>\n";

										print "</tr>\n";

									}

								}else{

									print "no pages found";

								}

							}else{

								print "failed to get pages";

							}

							

							print "</tbody>\n";

							print "</table>\n";

						}else{

							//error

							print "category not found";

						}

					}

				}else{

					//error

					print "state not found";

				}

			}else{

				//error

				print "error";

			}

		}else{

			if(isset($_GET['cat'])){

				//get value

				$cat = trim($_GET['cat']);	

				

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a state below to view papers.</h3>

							<table id="state">';

							

				//get states

				$query = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 1;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=specialty&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=specialty&cat=' . $cat . '&state=' . $rst['state_id'] . '">' . $rst['state_name'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}else{

				print '<h2>Specialty Papers</h2>

					   <h3>Pick a category below to view papers.</h3>

							<table id="category">';

				//get categories 

				$query = "SELECT cat_id, category FROM paper_categories ORDER BY category ASC";

				$result = mysql_query($query);

				if($result){

					//output info

					$count = 1;

					while($rst = mysql_fetch_array($result)){

						if($count%2){

							print '<tr><td width="300" class="st"><a href="?p=specialty&cat=' . $rst['cat_id'] . '">' . $rst['category'] . '</a></td>';

						}else{

							print '<td width="300" class="st"><a href="?p=specialty&cat=' . $rst['cat_id'] . '">' . $rst['category'] . '</a></td></tr>';

						}

						$count++;

					}

					if(mysql_num_rows($result)%2){

						print '<td>&nbsp;</td></tr>';

					}

				}

				print '</table>';

			}

		}

	}

?>

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20427055
Thank you so much for your help! I appreciate it..I understand what you did now much better and that helps me a ton..learning something everday. Thanks again!


I might have another question a bit later if you up to tackle it!

Thanks for your time!
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
ID: 31413235
Nice to work with..really kind and helpful.


Ryan
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20427066
Glad I could help :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now