Drewdle Posted January 5, 2011 Share Posted January 5, 2011 I'm currently trying...struggling....to teach myself PHP and I'm really bugged with this database stuff. I am slowly managing but I'm a tad bit stuck now. I want to show a specific piece of information from a table. Lets say my table is structured like so: iduseremail 1 Bob Bob@Name.com 2FredFred@Name.com 3MattMatt@Name.com What would I need to do to display ONLY Freds user? One way I tried only displayed the first rows info (Bob) the second way I tried (with a while loop) only displayed the last rows info (Matt) Heres my current code: <html> <body> <?php include 'dbwire.php'; $query = mysql_query('SELECT * FROM user'); $row = mysql_fetch_array($query); while ($row = mysql_fetch_array($query)) { echo '<b>User:</b> ' . $row['user'] . '<br />'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
nderevj Posted January 5, 2011 Share Posted January 5, 2011 You could add a "where" clause to your SQL query: SELECT * FROM user WHERE user = 'Fred' This will limit your query to only return rows that have a user field set to a value of "Fred". Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 <html> <body> <?php include 'dbwire.php'; $query = mysql_query('SELECT * FROM user'); while ($row = mysql_fetch_array($query)) { if($row[1] == 'Fred'){ echo '<b>User:</b> ' . $row[1] . '<br />'; } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
unlishema.wolf Posted January 5, 2011 Share Posted January 5, 2011 @jake2891 that isn't a very efficient way to do it. You will be better just using the "where" clause. Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 @jake2891 that isn't a very efficient way to do it. You will be better just using the "where" clause. true but if he wants all the results and only use freds results for something then he can do it that way Quote Link to comment Share on other sites More sharing options...
unlishema.wolf Posted January 5, 2011 Share Posted January 5, 2011 yes but what if fred isn't in $row[1]. You would need to check all of them. Btw I haven't coded in ages. Lol Okay I looked at it again. And its like I said I haven't coded in awhile. I'm wrong it looks in them all lol. Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 yes but what if fred isn't in $row[1]. You would need to check all of them. Btw I haven't coded in ages. Lol $row[1] = the table user field not fred Quote Link to comment Share on other sites More sharing options...
unlishema.wolf Posted January 5, 2011 Share Posted January 5, 2011 I don't know if you saw my edit but I edited my last post. Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 Thanks for the help jake2891. I've hit another snag. Is it possible to select a field (like 'title') from a link on a page then display another field from the same row (like 'content') below the links? Its like a page creator that you'd find on a cms but because I want to learn php rather than just copy it, I'd like to find some sort of sample I could play with. What I have is a new table (content) with 'id', 'title' and 'content' fields, what I want is a user to click a link (title field entry) and then display the content associated with the title. Example: id title content 1 index.php blahblahblah 2 about.php blahblahblah If they click a link named 'Main' it would collect content from id 1 and display it below. If you need more explination, let me know. Thanks on advance Quote Link to comment Share on other sites More sharing options...
unlishema.wolf Posted January 5, 2011 Share Posted January 5, 2011 You are basically wanting a link "main" that when clicked, it will display the info in the first row? Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 Not the entire info of all three fields (id, title, content) Just the info from one field. (content). I don't need to display the id or the title. But other than that...pretty much yeah. The same thing this chaps trying to accomplish: HERE Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 is this what u mean?? <html> <head> <script> function show_content(id){ document.getElementById(id).style.display='block'; } </script> </head> </html> <?php // sudo code foreach($results as $result){ // results holding your records from your content table $title = $result['title']; $id = $result['id']; $content = $result['content']; echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>"; echo "<div id=\"$id\" style=\"display:none\">$content</div>"; } ?> Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 I whipped it on there and it returned: Warning: Invalid argument supplied for foreach() in I don't know weather I included it in the wrong part of my script or missed something so I don't know if its what I mean...I don't know what the problem is to fix it and see. I think its because I haven't pre-defined the variable $results just after 'foreach' but thats just a guess? Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 lol... yeah man ull need to pull the content from ur table into the results variable and add in any other spacing and css you want to distance the links and divs etc.. let me know if u dont understand Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 Hmm...I don't understand? Atleast I don't think I do. So I should define my database query as a result like this: $results = mysql_query('SELECT * FROM content'); Or as an array thing like this: $query = mysql_query('SELECT * FROM contact'); $results = mysql_fetch_array($query); Or am I completely off? Lol. Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 yeah you can do this $query = mysql_query('SELECT * FROM contact'); while($results = mysql_fetch_array($query,MYSQL_ASSOC)){ $title = $results['title']; $id = $results['id']; $content = $results['content']; echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>"; echo "<div id=\"$id\" style=\"display:none\">$content</div>"; } Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 Cheers for your help pal but I figured a way to do it! $query = mysql_query("SELECT * FROM content WHERE id =1") or die(mysql_error()); $result = mysql_fetch_array( $query ); echo $result['title']; echo $result['content']; Well same thing really isn't it? What I need to do now is find a way to allow the visitor to select the id so I don't have to manually add each page...any ideas? I'm thinking...create a dropbox, post it to a variable and replace the id number with the variable?... Can I do that with a solid <a href=#></a> link instead of a dropdown? Ta. Quote Link to comment Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 no thats not rite because if the id is unique your fetching one record instead of an array of records? if your only wanting one record then use mysql_fetch_row instead. $query = mysql_query("SELECT * FROM content WHERE id =1") or die(mysql_error()); if($query){ $row = mysql_fetch_row($query); } // if your table is structure like this? $row[0]; // id $row[1]; // title $row[2]; // content Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 5, 2011 Author Share Posted January 5, 2011 I don't understand?...I only want to show one row info at a time, not all of the tables rows at the same time? (thats what your code looks like its going to do?)...have a look: The code I used above works fine except I have to manually define an id number in my query. The result of the code prints the page title and content onto the page just fine. [site url removed] Click 'Create Page' then add a title and create a simple html page in the content box. The next id number called up is going to be 7 so I'll edit the display page to show the info from id=7 so you can see how it works. Let me know when your done so I can remove this link as my database isn't protected because its just for me to practice on. Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 7, 2011 Author Share Posted January 7, 2011 is this what u mean?? <html> <head> <script> function show_content(id){ document.getElementById(id).style.display='block'; } </script> </head> </html> <?php // sudo code foreach($results as $result){ // results holding your records from your content table $title = $result['title']; $id = $result['id']; $content = $result['content']; echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>"; echo "<div id=\"$id\" style=\"display:none\">$content</div>"; } ?> Ok I tried using this method but again there a problems and I don't have a clue where to start looking in order to fix them... This is what happens: Here How would I be able to separate the links so it looks more like a listed navigation and also to only display one page. There are no spaces between the links and all though it will display the correct page when a link is clicked it does not replace it when the other link is clicked, only adds it to the page. Here's the full page code: <html> <head> <script> function show_content(id){ document.getElementById(id).style.display='block'; } </script> </head> <body> <font face=verdana size=1> <?php include 'dbwire.php'; $query = mysql_query('SELECT * FROM content'); while($results = mysql_fetch_array($query,MYSQL_ASSOC)){ $title = $results['title']; $id = $results['id']; $content = $results['content']; echo "<a href=\"javascript:void(0);\" onclick=\"show_content($id)\">$title</a>"; echo "<div id=\"$id\" style=\"display:none\">$content</div>"; } ?> <hr> </body> </html> How would I achieve what I need or atleast where would I need to look for the problem? Cheers. Quote Link to comment 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.