jeffmthomas Posted January 26, 2007 Share Posted January 26, 2007 Hello,Long time reader, first time poster. I have a little script that I am working on to basically pull download file information from a database and display a link to the download page on the form.Here is the SQL Table that I am working with:[code]CREATE TABLE downloads ( downloads_id int NOT NULL auto_increment, downloads_name varchar(64), downloads_description text, downloads_image varchar(208), downloads_url varchar(255) default NULL, PRIMARY KEY (downloads_id));[/code]Here is the PHP Code that I am working with:[code]<? /* Include Files *********************/session_start(); include("database.php");include("login.php");/*************************************/?><html><head><title>PG Mobile Site</title></head><body><?php require('header.php'); ?><?if($logged_in){$result = mysql_query( "SELECT downloads_name FROM downloads" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); while ($get_info = mysql_fetch_row($result)){ foreach ($get_info as $field) ?><center>There are <?php echo $num_rows; ?> downloads.<table width="250" border="0"><tr align="center"><td><font size="4"><?php echo $field; ?></font></td></tr></table></center><?}}else{?><p align="center">Bla Bla Bla you need to register and login before you can download.<? displayLogin(); ?></p><?}?><?php require('footer.php'); ?></body></html>[/code]When I print the <?php echo $field; ?>, which is the name of the download into the table, I want to make it a link that will link to the corresponding download ID in the same database table ... i.e. http://www.mysite.com/downloadlist.php?downloads_id=561. How would this be done as I am still in the learning process of PHP.Thanks in Advance,Jeff Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/ Share on other sites More sharing options...
fenway Posted January 26, 2007 Share Posted January 26, 2007 I'm not sure I understand what that foreach is doing... oh, i see... that's really ugly looking, breaking in & out like that. Anyway, instead of that, simply echo out your script path, then the uid. Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/#findComment-170032 Share on other sites More sharing options...
jeffmthomas Posted January 26, 2007 Author Share Posted January 26, 2007 Hello. Thank you for your quick reply. I put this together from tutorials and books that I am reading to learn PHP. How would I clean this up and get rid of all of the breaks in the code. Please advise as I would like to learn.Thank You,Jeff Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/#findComment-170072 Share on other sites More sharing options...
Hypnos Posted January 27, 2007 Share Posted January 27, 2007 [code]<?php /* Include Files *********************/session_start(); include("database.php");include("login.php");/*************************************/?><html><head><title>PG Mobile Site</title></head><body><?php require('header.php');if($logged_in){$result = mysql_query( "SELECT downloads_name FROM downloads" ) or die("SELECT Error: ".mysql_error()); ?><center>There are <?php echo mysql_num_rows($result) ?> downloads.<table width="250" border="0"><?phpwhile ($get_info = mysql_fetch_array($result)){ echo '<tr align="center">' echo '<td><font size="4">'; echo '<a href="page.php?id=' . $get_info['downloads_id'] . '">'; echo $get_info['downloads_name']; echo '</a></font></td></tr>';}?></table></center><?php}else{?><p align="center">Bla Bla Bla you need to register and login before you can download.<?php displayLogin(); ?></p><?php}require('footer.php'); ?></body></html>[/code]Basicly mysql_fetch_array will make an array of the current row, with the column name as the key. Each time it steps through the while loop, it gets the next row.Look through the code a couple of times. I cleaned it up in several spots.[size=7pt]Yes, I know I didn't need those echos. But I wanted to make easy to understand code.[/size] Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/#findComment-170509 Share on other sites More sharing options...
jeffmthomas Posted January 27, 2007 Author Share Posted January 27, 2007 Hello,Thank you very much for that. I now understand it much better, and I do understand that the echo was not necessary. I also know that I do not need to break in and out of the PHP as much. Thank you very much once again for helping me understand PHP better. YOu can only learn by making mistakes.I do have one last question though since I really dont feel like spending hours searching through my books and google to figure it out.Taking the above mentioned code. Say for example I have a downloads_cat option in the database table. How would I go about having it show only the rows that are in cat 1, or cat 5 for example.Thank a Million,Jeff Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/#findComment-170598 Share on other sites More sharing options...
jeffmthomas Posted January 27, 2007 Author Share Posted January 27, 2007 Couldnt edit is again. I did find my answer however. I just changed the query to include the WHERE bla = bla[code]$result = mysql_query( "SELECT downloads_name,downloads_id FROM downloads WHERE downloads_cat= 1" )[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35863-solved-making-link-from-database-table/#findComment-170686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.