|
|
|
09-24-2002, 05:48 PM
|
Postid: 73995
|
|
Registered User
Join Date: Mar 2001
Posts: 161
|
PHP_SELF variable
Having some trouble getting access to a variable ($cat_id) after it has been passed through a PHP_SELF form. The script works fine (needs some tweaking but generally ok) up to the second SQL statement: $queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WH EREÂ*cat_idÂ*= '$_POST[cat_id]'";
What I get is a blank (no errors) unless I hard code it to, for example:
$query = "SELECT DISTINCT article_title, id, cat_name FROM News WHERE cat_id = '1'";
In this case I do get a result. Anybody have any idea why this variable is not getting passed on? I've tried a few different ways but it has not worked unless I hard code it!
Here is my current code:
PHP Code:
<?php require('/pagedresults.php'); ?>
<?php
//dbConnect("****");
$cnx = @mysql_connect('localhost','root','****');
mysql_select_db('****',$cnx);
$rs = new MySQLPagedResultSet("select date_format(date, '%M %D, %Y') AS nicedate,id, article_title, cat_name, cat_id, article from News ORDER BY date DESC", 10,$cnx);
?>
<?php
if(!$_POST['submit'])
// form not yet submitted, display search categories form
{
?>
<table>
<?php while ($row = $rs->fetchArray()): ?>
<tr><td><a href=news_story.php?id=<?=$row['id']?>><?=$row['article_title']?></a></td></tr>
<tr><td><?=$row['cat_name']?>, <?=$row['nicedate']?></td></tr>
<?php endwhile; ?>
</table>
<p><?=$rs->getPageNav("id=$id")?></p>
<form action="<? echo ($_SERVER['PHP_SELF']); ?>" method="post">
<select name="newsid">
<?php
// get list of news categories
$cnx = @mysql_connect('localhost','root','*****');
$db = '****';
$query = "SELECT DISTINCT cat_id, cat_name from News";
$result = mysql_db_query($db, $query, $cnx) or die ("Error in query: $query. " . mysql_error());
// and print
while(list($cat_id, $cat_name) = mysql_fetch_row($result))
{
echo "<option value=$cat_id>$cat_name</option>";
}
mysql_free_result($result);
?>
</select>
<input type="submit" name="submit" value="Go!">
<?php
}
if($_POST['submit'])
{
// get list of news categories
$cnx = @mysql_connect('localhost','root','****');
$db = '******';
$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_idÂ*= '$_POST[cat_id]'";
$query = "SELECT DISTINCT article_title, id, cat_name FROM News WHERE cat_id = '$cat_id'";
// execute query
$result = mysql_db_query($db, $query, $cnx) or die ("Error in query: $query. " . mysql_error());
// number of records found
$count = mysql_num_rows($result);
?>
Your search returned <? echo $count; ?> match(es)?>
<p>
<ul>
<?php
// list matches
while(list($article_title, $id) = mysql_fetch_row($result))
{
echo "<li><a href=news_details.php?id=$id>$article_title</a>";
}
?>
</ul>
<?php
}
// clean up
mysql_close($cnx);
?>
|
|
|
09-24-2002, 06:00 PM
|
Postid: 73996
|
|
Registered User
Join Date: Mar 2000
Location: MWV
Posts: 3,986
|
I haven't looked through the full code, but you might need to bust out of the quotes in the SQL query you posted:
Code:
$query = "SELECT article_title, id, cat_name FROM News WHERE cat_id = '". $_POST[cat_id] ."'";
You could probably also use $_REQUEST in place of $_POST if you know the PHP installation is recent and want a bit more flexibility.
Dan
|
|
|
09-24-2002, 06:10 PM
|
Postid: 73997
|
|
Registered User
Join Date: Mar 2001
Posts: 161
|
Hi Dan,
Thanks for the tip. I tried your code and it does not produce any errors but nothing else either!
The code is in three parts:
1) Get the ten latest news stories from the database and list the rest within a next/previous page structure
2) put up a drop down box with the news categories available from the database, after checking first with the database
3) If a user selects one of the categories from the drop down list then display the results as links
It works basically, just can't get no.3 to work because my little variable is not getting passed on! 
|
|
|
09-24-2002, 06:47 PM
|
Postid: 74000
|
|
Registered User
Join Date: Mar 2000
Location: MWV
Posts: 3,986
|
Hmm, I don't see anything that would keep the variable from being passed through (the style of coding is much different than my own, so I'm having a bit of a hard time looking for clues as to what might be missing), so I would try echoing $query to the screen upon submission to see what, if anything, it is receiving.
Dan
|
|
|
09-24-2002, 11:19 PM
|
Postid: 74016
|
|
Mostly Harmless
Join Date: Jan 2000
Location: Chicago, IL,USA
Posts: 1,866
|
It's amazing how hard this was to see. I looked at it about twelve times...
You're not setting a POST variable named "cat_id" to come back . You're setting a POST variable named "newsid" to come back.

|
|
|
09-24-2002, 11:30 PM
|
Postid: 74017
|
|
Registered User
Join Date: Mar 2000
Location: MWV
Posts: 3,986
|
Brilliant! I actually looked for that very thing, but I was thrown off by "<option value=$cat_id>$cat_name</option>";. I suppose it would help to have $cat_id in the right place, huh?
Dan
|
|
|
09-25-2002, 03:30 AM
|
Postid: 74024
|
|
Registered User
Join Date: Mar 2001
Posts: 161
|
Wow that's great! Can't believe I missed that! Thanks Dan and Paul for helping out. It works now beautifully.
The first part of the code is from a Kevin Yank article on Sitepoint for returning next/previous page results, and the rest is also borrowed as I did not know how to get a drop down box based on what was in the database. This is why the code looks messy - I just have to clean it up now.
One thing from the SQL statement - I tried this but it doesnt work:
PHP Code:
$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_idÂ*= '$_POST[cat_id]'";
Dan's sql does work:
PHP Code:
$query = "SELECT article_title, id, cat_name FROM News WHERE cat_id = '". $_POST[cat_id] ."'";
Shouldn't the first statement work (what's wrong with it, I get a parse error)? What is the difference?
|
|
|
09-25-2002, 03:32 AM
|
Postid: 74025
|
|
Registered User
Join Date: Mar 2001
Posts: 161
|
This one didn't work either:
PHP Code:
$queryÂ*=Â*"SELECTÂ*article_title,Â*id,Â*cat_nameÂ*FROMÂ*NewsÂ*WHEREÂ*cat_idÂ*= '$_POST['cat_id']'";
|
|
|
09-25-2002, 10:24 AM
|
Postid: 74031
|
|
Mostly Harmless
Join Date: Jan 2000
Location: Chicago, IL,USA
Posts: 1,866
|
PHP Rules on parsing strings
For an array, that means:
PHP Code:
$query = " SELECT article_title, id, cat_name FROM News WHERE
cat_id = '{$_POST['cat_id']}'";
Maybe...
Last edited by PaulKroll : 09-25-2002 at 10:41 AM.
|
|
|
09-25-2002, 11:36 AM
|
Postid: 74033
|
|
Registered User
Join Date: Mar 2000
Location: MWV
Posts: 3,986
|
Paul's way should work, too. I prefer to "step outside" the quotes for such things to make it a bit easier to follow visually, but same net effect.
Dan
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 visitors)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 08:10 AM.
|
| |
|
|
|