Maracles Posted October 18, 2009 Share Posted October 18, 2009 I am working on a personal website that in part contains a small 'shares' profile, one of the pages of which will list all owned shares in a single table. Each row is a unique share. The information is called from the database on page load. This table provide only summary information on each share and the user must click a 'view' button next to each share to go to that individual stocks profile with more info. My problem is that I don't know how to link the 'View' button with the actual stock so that when it is clicked the user is presented with a page with that shares detailed info. When a user clicks 'View' the page must know firstly which row of the table and which stock the user has clicked view for (which I don't know how to do) and then post a variable which identifies the stock to the following page which will be a template whose values simply change based on the chosen stock. Can anyone help? I can show code if needed however it is a bit of a mess at the moment! Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/ Share on other sites More sharing options...
Warz Posted October 18, 2009 Share Posted October 18, 2009 When you echo out the stocks also echo out and "id" or something that can identify each stock. I do like this: echo '<a href="viewstock.php?id='.$row['id'].'">View</a>'; Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939087 Share on other sites More sharing options...
Maracles Posted October 18, 2009 Author Share Posted October 18, 2009 Thank you for your reply. I understand the concept but i'm not too sure how to include that code. I have included some of my actual code if this helps. It is worth noting that each share already has a unique id in its 'cxcode'. for ($i=0; $i < $num_results; $i++) { $row = mysql_fetch_assoc ($result); echo '<tr><td>'; echo '<p><strong>'.($i+1).'.</td><td>'; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong></td><td>"; echo stripslashes($row['cxcode']); echo '</td><td>'; echo stripslashes($row['reldate']); echo '</td><td>'; echo stripslashes($row['strtdate']); echo '</td><td>'; echo stripslashes($row['enddate']); echo '</td><td>'; echo '<form action="stckprofile.php"><input type="submit" value="View" name="stckview"></form>'; echo '</td><td>'; echo '<form action=""><input type="submit" value="Delete" name="stckdel"></form>'; echo '</td>'; Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939097 Share on other sites More sharing options...
Warz Posted October 18, 2009 Share Posted October 18, 2009 for ($i=0; $i < $num_results; $i++) { $row = mysql_fetch_assoc ($result); echo '<tr><td>'; echo '<p><strong>'.($i+1).'.</td><td>'; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong></td><td>"; echo '<a href="viewstock.php?id='.stripslashes($row['cxcode']).'">View</a>'; echo '</td><td>'; echo stripslashes($row['reldate']); echo '</td><td>'; echo stripslashes($row['strtdate']); echo '</td><td>'; echo stripslashes($row['enddate']); echo '</td><td>'; echo '<form action="stckprofile.php"><input type="submit" value="View" name="stckview"></form>'; echo '</td><td>'; echo '<form action=""><input type="submit" value="Delete" name="stckdel"></form>'; echo '</td>'; Then on viewstock.php you just need to get the variable like this: $cxcode = $_GET['id']; Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939099 Share on other sites More sharing options...
Maracles Posted October 18, 2009 Author Share Posted October 18, 2009 Ok, I see what you mean however because I'm still very much a PHP beginner I have one more problem. I have never worked with the URL like the one you use i.e. with the '?id=' and having tried your code the link will not connect to the viewstock.php page. The link 'appears to be broken'. When you add an '?id' to a link does it change the way you have to link to the page? Thanks again, this is helping me big time! Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939101 Share on other sites More sharing options...
Warz Posted October 18, 2009 Share Posted October 18, 2009 Hi, you need to create the viewstock.php page yourself... After any php file you can add ?anything=variable - this is simply to send variables to another page. You can add many variables by using &... example: ?name=john&age=20&userid=28294 then you can get these variables on the page you visit. Say you link to viewstock.php?name=john&age=20&userid=28294 then you can create viewstock.php and get these variables like this: <?php $username = $_GET['name']; $userage = $_GET['age']; $userid = $_GET['userid']; ?> Now you can also use these variables to make a new SQL query and get stock info (viewstock.php?id=10242): <?php $stockid = $_GET['id']; $sql = "select * from stocks WHERE id = ".mysql_real_escape_string($stockid).""; $res = mysql_query($sql); .... ?> Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939102 Share on other sites More sharing options...
Maracles Posted October 18, 2009 Author Share Posted October 18, 2009 A-ha! Firstly, I found my problem which was that the url had a typo which was why it wasn't linking, I have now changed this and it links and the viewstock.php page is updating as desired. Secondly, that is extremely useful info regarding the links and variables that I did not know about! I'm currently travelling and do not have access to some of the PHP tutorial books I have been using so have been struggling to find out methods I don't already know. Thirdly, thanks again for the help, that was the last big problem I had with developing the page basics and have been trying for ages to figure it out. I will mark this solved. Link to comment https://forums.phpfreaks.com/topic/178104-solved-retrieving-rows-from-database-problem/#findComment-939103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.