Jump to content

Recommended Posts

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.

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 ?

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

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>';
?>

<?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!  :D  :D

 

Mark

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.