Jump to content
mahenda

How to redirect this links to the specific page?

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

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I understand what the problem is. Do you understand what I said the solution is?

Share this post


Link to post
Share on other sites
18 hours ago, requinix said:

I understand what the problem is. Do you understand what I said the solution is?

No

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

thank you guys, it is working, i added alias on each table column and using if condition to detect if the data come from first table redirect to first page else second page

Share this post


Link to post
Share on other sites

But i have this question why I get undefined variable,  when the field in mysql database is set to null value/type.

Share this post


Link to post
Share on other sites

You get an undefined variable warning because the variable was not defined. Since you haven't posted the code for it, my guess is that you might have misspelled its name somewhere.

  • Like 1

Share this post


Link to post
Share on other sites

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.