Jump to content

How to redirect this links to the specific page?


mahenda

Recommended Posts

I have two table in my database combined by using  UNION, when it come to searching via search form, all data are retrieved as expected, but the problem come when i click the link that has the data from second table it redirecting to the page with first table details.

Assume i have  retrieved data using while loop and the UNION.

 while($search = $query->fetch()) {?> 
       <div>
       	<a href="pageone.php?po=<?php echo $search['pr_id'];?>">Read More...</a>             
       </div>
<?php }?>

//the result become

<a href="pageone.php?po=1">Read More...</a>             
<a href="pageone.php?pt=3">Read More...</a>

//But what if data come from both first and second table in a database, i want the link become to be as

<a href="pageone.php?po=1">Read More...</a>             
<a href="pagetwo.php?pt=3">Read More...</a>

// any idea please

 

 

Link to comment
Share on other sites

20 hours ago, requinix said:

You have to put something in your query so that you can tell where each result is coming from. Then use that to output the correct link.

My query look like this

SELECT pr_id, pr_name FROM pr WHERE pr_name LIKE :q UNION SELECT pt_id, pt_name FROM pt WHERE pt_name LIKE :q

 

The problem is when we search it combine all pr_id and pt_id in a single column and all I'd can be retrieved by using pr_id.

So when user click the link which carry pt_id it redirect to the page with pr_id details.

Help

Link to comment
Share on other sites

On 7/4/2020 at 8:31 AM, mahenda said:

My query look like this

SELECT pr_id, pr_name FROM pr WHERE pr_name LIKE :q UNION SELECT pt_id, pt_name FROM pt WHERE pt_name LIKE :q

Look at what that query does. It returns an ID and a name. But how do you know what table they came from? You don't.

You need to put something into the query that tells you the table. Something that is different for the two table sources. For example, one could use the string "pr" and the other "pt". Or maybe a nicer value than those, such as something I wouldn't know because "pr" and "pt" are terrible names for guessing about what their tables are for. Or why there are two tables as similar as they are.

Link to comment
Share on other sites

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.