nortksi Posted August 9, 2011 Share Posted August 9, 2011 Hi guys, may I request some help with a problem I'm having trying to pass results retrieved from a mySQL database as variables in a URL within a loop. At the moment the connection to the database and the query work fine and I can display these results to the screen. Now I want to be able to click on one of these results to query the database again. However the URL being created is: http://www.domainname.co.uk/script.php?event=Array[event_name]&town=middlesbrough As you can see the town is being passed fine but the event name is not. Below is my code: $results=@mysql_num_rows($rt); if ($results > 0) { for ($ii = 0; $ii < $results; $ii++){ $data = mysql_fetch_array($rt); $dr [] = $data; } } if ($results > 1) { $location = $postLoc; for ($ii = 0; $ii < $results; $ii++){ echo '<a href="script.php?'."event=$dr[$ii][event_name]&town=$location".'">'.$dr[$ii]['event_name'].'</a>';?><br /><?php } } I am just a novice so this is probably easily fixed, I would greatly appreciate any help offered. Kind regards, Mark. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 try something like this: $rt=mysql_query("YOUR DATABSE QUERY HERE"); while($dr = mysql_fetch_array($rt)){ echo '<a href="script.php?event='.$dr['event_name'].'&town='.$postLoc.'">'.$dr['event_name'].'</a><br>'; } P.S. where does $postLoc come from ? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 the variable $data is already an array, so setting it to $dr[] as well is redundant.. now i'm not sure if you want all of the results stored into one URL or if you want each result to produce its own unique URL..? what Webstyles suggested should work if you want a unique URL for each query result, however if you want a single URL, we will need to come up with something different here Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 9, 2011 Share Posted August 9, 2011 You'll need to wrap the multidimensional array in curly quotes: <?php echo '<a href="script.php?'."{event=$dr[$ii][event_name]}&town=$location".'">'.$dr[$ii]['event_name'].'</a>'; ?> Or you could move the array outside of the double-quoted string: <?php echo '<a href="script.php?event=' . $dr[$ii]['event_name'] . '&town=' . $location . '">' . $dr[$ii]['event_name'] . '</a>'; ?> Quote Link to comment Share on other sites More sharing options...
nortksi Posted August 9, 2011 Author Share Posted August 9, 2011 <?php echo '<a href="script.php?event=' . $dr[$ii]['event_name'] . '&town=' . $location . '">' . $dr[$ii]['event_name'] . '</a>'; ?> Thanks for your reply guys! I tried cyberRobot's second suggestion and that worked a treat! Thanks again guys! Mark Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 you can eliminate some unessecary steps by using $data and while loop here instead of a multidimensinal array and for loop.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.