get link to open within php page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

get link to open within php page

Post by unexperion »

I suppose this is simple but i fail to find the right definition for it - so I can find it/search for it successfully... So I have to write it like this:
First. I have two pages, index.php and articles.php.
(Of course, and a MySQL database with some articles in it. Tables are : art_id, art_name, art_img, art_txt).

Now, both pages have a simple table on it, with two tables/cells - page articles.php should have a table/cell (named: result) and a table/cell (named: links)

In this cell "links" I have an ordered list of hyperlinks echoed from MySQL database

Code: Select all

echo "<a href=\"articles.php?id=$row[art_id]\">".$row["art_name"]."</a><br/>";
That is working all right. A list is being displayed with only names as I wanted.

But I want - when I click on every individual link there that an article with appropriate ID opens in "result" table with image, name, and txt attributes listed.
So, my question is - how do I make a query (php code) to get their links open in the (same or some other) page - only in the location table that I call "result"?

Please, help me, what would be appropriate php code for this?
Thank you!!!
Last edited by unexperion on Tue Nov 15, 2011 1:41 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: get link to open within php page

Post by social_experiment »

unexperion wrote:how do I make a query (php code) to get their links open in page articles.php - only in the location table "result"?
Firstly, it seems like the article.php page is already used; secondly i'm guessing here because i don't quite understand your question: Select the details from the 'result' table, this would give you results from the 'result' table
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

I perhaps wasn't clear - 'result' table isn't a part of MySQL database it's a name of the table on the page, a zone where I would like to have displayed desired data from the database!!! I will edit my first post so it's more clear... sorry I'm such a complete newb :( (I suppose this is all pretty generic for you - it's a simple matter of "you have a link and you chose items from the dbase to display - now go and display that result etc" ... yes... But how? :) :D ). Please help!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: get link to open within php page

Post by social_experiment »

I've done a quick test using anchors which could help

Code: Select all

<!-- create an anchor -->
<a name="result" id="result" ></a>
<?php
 if (isset($_GET['id'])) {
    // print data
 }
 //
?>
When you create the link add '#result' to the end of it; when the link is clicked it goes to the specific point in the page and your php code is tested to see if a $_GET variable is set.

Code: Select all

echo "<a href=\"articles.php?id=$row[art_id]#result\">".$row["art_name"]."</a><br/>";
Another alternative is to use javascript but i'm not well versed in that so i'll leave it to other forum member/s
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

Sorry - I get nothing. I see anchor, and i see there is a php code on the page, but when I run it on the server - in that place I see only empty space... The links on the other table are ok, they point with #result now, but nothing happens in my "result" table. *maybe to show me how to display other data besides [id] there?
I need data from dbase under art_name, art_img and art_txt. How would $GET all of them line look? Maybe that would work? Thank you for your trouble!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: get link to open within php page

Post by social_experiment »

unexperion wrote:I see anchor, and i see there is a php code on the page, but when I run it on the server - in that place I see only empty space
The anchor shouldn't be visible; Retrieving the data is simple, you are already doing it in your existing code

Code: Select all

<?php
 if (isset($_GET['id'])) {
   $id = $_GET['id'];
  // dbase under art_name, art_img and art_txt.
   $qry = "SELECT art_name, art_img, art_txt FROM yourTable WHERE id = $id";
  //
 }
?>
This example doesn't check the $_GET value but you should.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

Something like this?

Code: Select all

<?php
 if (isset($_GET['id'])) {
   $id = $_GET['id'];
  // dbase under art_name, art_img and art_txt.
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("articles_db") or die(mysql_error());
   $query = "SELECT art_name, art_img, art_txt FROM articles WHERE art_id = $id";
  //
 }
?>
it's not working... I still see nothing. do I have to add an echo line too?

when i add:

Code: Select all

   echo $_GET['id'];
under $query = "SELECT........ i got the art_id displayed!!! but nothing else :(

p.s.
(when i said i see anchor - i meant in I see it in the editor - not in the browser :D !!!)
Last edited by unexperion on Tue Nov 15, 2011 3:40 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: get link to open within php page

Post by social_experiment »

Code: Select all

<?php
// rest of the code ^
$query = "SELECT art_name, art_img, art_txt FROM articles WHERE art_id = $id";
$sql = mysql_query($query);

while ($array = mysql_fetch_array($sql)) {
 /*
 echo $array['art_name'];
 echo $array['art_img'];
 echo $array['art_txt'];
*/
}
?>
The commented area will be where you display the results
unexperion wrote:(when i said i see anchor - i meant in I see it in the editor - not in the browser !!!)
shot
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

I still see only id. it's now in the right place, and for each different article is displaying it's id number : i see like, only, 14 or 23 or 18... nothing else :(

btw THANK YOU THANK YOU THANK YOU for helping me!!!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: get link to open within php page

Post by Celauran »

Perhaps if you post the latest iteration of the relevant code we could better see what's going wrong.
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

Ok - one more time:
I have a dbase named articles_db.
The dbase has tables called art_id, art_name, art_img, art_price and art_txt. There are some items in them.
I have a php page named articles.php
On that page I have a table with 2 cells. Once cell is named "links" and the other "result". In result there is a anchor called "result" also.
Link cell is a link zone.
In it there are several links (hrefs) that call out to individual id's of the dbase,
..... =\"articles.php?id=$row[art_id]#result.....
That all works fine. each link goes to the desired id of the database article. That is not important. Links cell is not relevant.

What is important is the "result" cell.
Now, I have this code inside:

Code: Select all

<?php
 if (isset($_GET['id'])) {
   $id = $_GET['id'];
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("articles_db") or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
   $query = "SELECT art_id, art_name, art_img, art_txt, FROM articles WHERE id = $id";
   $sql = mysql_query($query);
   echo $_GET['id'];
 }
?>
When I click on any link in the link cell, in result cell i see only ID number of the selected item. I click next this number changes. So I have one $GET right.
Ok. But I want to see all 5 table results for asked ID. I dont know how :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: get link to open within php page

Post by Celauran »

Why are you echoing $_GET['id']? Also, you don't seem to be doing anything with your MySQL Result $sql.
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

Ok, help me out, please - don't just go and criticize me :) ... Please, write something out ; I tried to echo (after echo $_GET['id'];) all rows from articles dbase but nothing appears.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: get link to open within php page

Post by Celauran »

I wasn't trying to criticize, just point out a possible oversight. You clearly know how to iterate through SQL results as you've done it on your other page.

What about this?

Code: Select all

while ($row = mysql_fetch_assoc($sql))
{
    echo $row['art_id'] . "<br />";
    echo $row['art_name'] . "<br />";
    etc.
}
User avatar
unexperion
Forum Newbie
Posts: 16
Joined: Mon Nov 14, 2011 6:38 pm

Re: get link to open within php page

Post by unexperion »

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given ... i placed this below query... please, can you combine it with my previous sent code? maybe i placed it in a wrong place?

p.s. I know you're not criticizing, I'm kidding :) :drunk:
Post Reply