Matic Posted August 25, 2013 Share Posted August 25, 2013 Hey, I want to list all of my database entries to a nice html format. For every unique id there is one row displayed out from my database to my php/html. I just don't know how to do that. I tried using while loop and foreach I just don't know how to properly structure them... My query and php code looks like this: $query = mysql_query("SELECT * FROM sport"); while($row = mysql_fetch_array($query)) { $result = $row['sport_ime'].' '.$row['sezona']; } Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/ Share on other sites More sharing options...
fastsol Posted August 25, 2013 Share Posted August 25, 2013 $query = mysql_query("SELECT * FROM sport"); while($row = mysql_fetch_array($query)) { echo $row['sport_ime'].' '.$row['sezona'].'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446710 Share on other sites More sharing options...
Matic Posted August 25, 2013 Author Share Posted August 25, 2013 $query = mysql_query("SELECT * FROM sport"); while($row = mysql_fetch_array($query)) { echo $row['sport_ime'].' '.$row['sezona'].'<br>'; } Â no no, echo row just lists entire database in one row like so: "foo1988bar2000etc..." I want each new id in a new row ... I know there is something with for each() statement I just can't format it right Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446713 Share on other sites More sharing options...
fastsol Posted August 25, 2013 Share Posted August 25, 2013 Did you even try what I posted, I have a line break after each row so they are all on new lines. You will need to be a bit more specific as to how you want it formatted than simply saying you want it on a new row, cause that is what I gave you. Do you want it in a new table row or simply a new line on the screen, what? Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446715 Share on other sites More sharing options...
Matic Posted August 25, 2013 Author Share Posted August 25, 2013 (edited) Did you even try what I posted, I have a line break after each row so they are all on new lines. You will need to be a bit more specific as to how you want it formatted than simply saying you want it on a new row, cause that is what I gave you. Do you want it in a new table row or simply a new line on the screen, what? Oh damn I didn't see that break tag there, my bad! Would you also be so kind to post a solution, if you know one, how to make every row clickable so that user can access entries and modify/delete them?  Also does anyone know how to store that line echo $row['sport_ime'].' '.$row['sezona'].'<br>'; inside $_SESSION variable correctly? Edited August 25, 2013 by Matic Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446732 Share on other sites More sharing options...
jcbones Posted August 25, 2013 Share Posted August 25, 2013 Â <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { //if page is posted, then form was submitted. Â $update = sprintf("UPDATE sport SET sport_ime='%s', sezona='%s' WHERE id=%d", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mysql_real_escape_string($_POST['sport_ime']), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mysql_real_escape_string($_POST['sezona']), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $_GET['id']); //put values into proper formatted, and escaped string. Â if(!mysql_query($update)) { //either query successfully. Â Â echo mysql_error(); //or error out. Â } } $sql = "SELECT * FROM sport"; //build the main query. if(!emtpy($_GET['id']) { //if the $_GET index is present, then we need to get just that record. $edit = 1; //specify this page is an edit. Â $id = (int)$_GET['id']; //cast value to integer. Â $sql .= "WHERE id = $id"; //add the where clause to the query. } $query = mysql_query($sql); //run the query. while($row = mysql_fetch_array($query)) //fetch results. { if(isset($edit)) { //if this page is an edit page. Â echo '<form action="" method="post">' . "\n" Â Â Â Â .'<input type="hidden" name="id" value="' . $row['id'] . '" \>' . "\n" Â Â Â Â .'<label for="sport_ime">Sport IME</label>' . "\n" Â Â Â Â .'<input type="text" name="sport_ime" value="' . $row['sport_ime'] .'"/>' . "\n" Â Â Â Â .'<label for="sezona">Sezona</label>' . "\n" Â Â Â Â .'<input type="text" name="sezona" value="' . $row['sezona'] . '">' . "\n" Â Â Â .'<input type="submit" name="submit" value=" SUBMIT " />' . "\n" Â Â Â .'</form>'; //build the form. Â } else { //if it isn't. Â echo $row['sport_ime'].' '.$row['sezona'].'<a href="?id=' . $row['id'] . '">Edit</a><br>'; //show the rows with a clickable edit link (which reloads the page and starts over). Â } } Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446738 Share on other sites More sharing options...
Matic Posted August 25, 2013 Author Share Posted August 25, 2013 wow jcbones, is that the code for the entire page? Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446740 Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 (edited) Oh damn I didn't see that break tag there, my bad! Would you also be so kind to post a solution, if you know one, how to make every row clickable so that user can access entries and modify/delete them? Â Also does anyone know how to store that line echo $row['sport_ime'].' '.$row['sezona'].'<br>'; inside $_SESSION variable correctly? Â Â Fetch all rows and push it inside an associative array. Â Try, $query = mysql_query("SELECT * FROM sport"); $_SESSION = array(); while($row = mysql_fetch_array($query)) { $_SESSION[] = array($row['sport_ime'],$row['sezona']); } // dump the data echo '<pre>'.print_r($_SESSION, true).'</pre>'; Or....for best performance use mysql_fetch_assoc() function. while($row = mysql_fetch_assoc($query)) { $_SESSION[] = array($row['sport_ime'],$row['sezona']); } Edited August 26, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446742 Share on other sites More sharing options...
Matic Posted August 26, 2013 Author Share Posted August 26, 2013 Â Fetch all rows and push it inside an associative array. Â Try, $query = mysql_query("SELECT * FROM sport"); $_SESSION = array(); while($row = mysql_fetch_array($query)) { $_SESSION[] = array($row['sport_ime'],$row['sezona']); } // dump the data echo '<pre>'.print_r($_SESSION, true).'</pre>'; Or....for best performance use mysql_fetch_assoc() function. while($row = mysql_fetch_assoc($query)) { $_SESSION[] = array($row['sport_ime'],$row['sezona']); } Â That is all fine and dandy but if I call this session in another file and try to echo it, it still displays only the last element of the array... Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446748 Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 (edited) How did you echo it? Can I see the code? Also show us the result of: Â // dump the data echo '<pre>'.print_r($_SESSION, true).'</pre>';Â Edited August 26, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446749 Share on other sites More sharing options...
Lawlssbatmbl Posted August 26, 2013 Share Posted August 26, 2013 I used this you tube php development lessions to help me get the basics Lesson 39 - 40 is probably what your looking for. Â http://www.youtube.com/watch?v=od0UM78JXg0&list=PL6627A6DA3C3A772E Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446751 Share on other sites More sharing options...
Matic Posted August 26, 2013 Author Share Posted August 26, 2013 Â How did you echo it? Can I see the code? Also show us the result of: Â // dump the data echo '<pre>'.print_r($_SESSION, true).'</pre>';Â Â Well yes I echo in my html file like so: <?php echo($_SESSION['result']); ?> in my backend php I do the session storage like so: while($row = mysql_fetch_array($query)) { $_SESSION['result'] = $row['sport_ime'].' '.$row['sezona'].'<br>'; } // and then redirect back to html where it should echo the stuff header('Location:../view/vnos.php',true,303); exit; The results you requested were just array => 0 and 1 for the last element in my array Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446755 Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 (edited) No, you're using sessions. Go to php.net and check out how to.....  Try next:  main.php <?php session_start(); $query = mysql_query("SELECT * FROM sport") or die(mysql_error()); $outputs = array(); while($row = mysql_fetch_array($query)) { $outputs[] = array($row['sport_ime'],$row['sezona']); } $_SESSION['output']=$outputs; // and then redirect back to html where it should echo the stuff header('Location:../view/vnos.php',true,303); exit; vnos.php <?php session_start(); echo '<pre>'.print_r($_SESSION['output'], true).'</pre>'; You should be consider how to use a session_start() and how to destroy that session.  Go to php.net and come back again!  EDIT: session_start() should be on very top of the page!  I've made a typo -> $outputs[] = array($row['sport_ime'],$row['sezona']); Edited August 26, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446756 Share on other sites More sharing options...
Lawlssbatmbl Posted August 26, 2013 Share Posted August 26, 2013 Something like this . echo "<table align=\"center\" width=\"500\" border=\"0\" background=\"images/FormBG.png\">"; echo "<tr><td colspan=\"5\" height=\"50\"><font size=\"5\">Excision Type</font><td></tr>"; echo "<tr><th>ID</th><th>Type</th><th>Name</th></tr>"; while ($stmt->fetch()) { echo "<form method=\"post\" action=\"excision.php\"> "; echo "<tr><td><input name=\"excision_id\" type=\"hidden\" value=" . $excision_id . ">" . $excision_id . "</td>" ; echo "<td><input name=\"excision_type\" type=\"text\" value=" . $excision_type . "></td>"; echo "<td><input name=\"excision_name\" type=\"text\" value=" . $excision_name . "></td>"; echo "<td><input name=\"edit\" src=\"/IC/images/Clipboard 3.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Edit\"></td>"; echo "<td><input name=\"delete\" src=\"/IC/images/Close - Cancel 1.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Delete\"></td></tr>"; echo "</form>"; } echo "<form method=\"post\" action=\"excision.php\"> "; echo "<tr><td> </td>" ; echo " <td><input name=\"a_excision_type\" type=\"text\"></td>"; echo " <td><input name=\"a_excision_name\" type=\"text\"></td>"; echo " <td><input name=\"add\" src=\"/IC/images/Add Green.png\" type=\"image\" style=\"height:24px; width:24px \" value=\"Add\"></td>"; echo " <td></td></tr>"; echo "</form>" ; echo "</table>" ; Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446775 Share on other sites More sharing options...
jazzman1 Posted August 26, 2013 Share Posted August 26, 2013 @Lawlssbatmbl, are you a big microsoft fan? I've never seen such beautiful backslashes in my life  Irrelevant reply! Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446777 Share on other sites More sharing options...
Lawlssbatmbl Posted August 26, 2013 Share Posted August 26, 2013 Stickler for Syntax maybe , but no Microsoft fan. Unix anyday of the week. I just found this gottcha with one of the lines where I had a value with two words with a space between. Saved to the database ok , but when it redrew the table it only showed the first word. Fixed it with this tricky "\" combo. Â Before echo "<td><input name=\"excision_type\" type=\"text\" value=" . $excision_type . "></td>"; After echo "<td><input name=\"excision_type\" type=\"text\" value=\"" . $excision_type . "\"></td>"; Shows you the importance of those double quotes. lol Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446780 Share on other sites More sharing options...
fastsol Posted August 26, 2013 Share Posted August 26, 2013 Or you could use single quotes and not have to deal with excaping. echo '<td><input name="excision_type" type="text" value="' . $excision_type . '"></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446827 Share on other sites More sharing options...
Matic Posted August 26, 2013 Author Share Posted August 26, 2013 (edited) Thanks for all the help but my session variable still shows only the last element of my row. When it is echoes directly it is okay but when it is stored in session it only displays the last element of array... Â I make my session variable store array like so: $outputs = array(); while($row = mysql_fetch_array($query)) { $_SESSION['row_id'] = $row['id']; $outputs = $row['sport_ime'].' '.$row['sezona'].'<a href=../view/modifikacije.php> Edit</a><br>'; $_SESSION['output'] = $outputs; } and then on another page I call that variable like so: $outputs = $_SESSION['output']; echo ($outputs); It just echoes one row! It is the last entry in my database, why doesn't it want to echo everything? People at another site say that I am repeatedly overwriting the same variables! But I don't know how to avoid that Edited August 26, 2013 by Matic Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446843 Share on other sites More sharing options...
fastsol Posted August 26, 2013 Share Posted August 26, 2013 It's cause you are reassigning the value of the output with every loop. $_SESSION['output'][] = $outputs; // Need the [] so that it adds a new array element rather than over-writing the previous output. Then you would need to either call a specific array key or use a foreach loop on the $_SESSION['output'] to echo out the outputs. Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446845 Share on other sites More sharing options...
Matic Posted August 26, 2013 Author Share Posted August 26, 2013 It's cause you are reassigning the value of the output with every loop. $_SESSION['output'][] = $outputs; // Need the [] so that it adds a new array element rather than over-writing the previous output. Then you would need to either call a specific array key or use a foreach loop on the $_SESSION['output'] to echo out the outputs. yes this! Now how do I do this properly with foreach statement? This is what I am asking this whole time, though other answers in this thread helped me greatly also Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446846 Share on other sites More sharing options...
fastsol Posted August 26, 2013 Share Posted August 26, 2013 foreach($_SESSION['output'] as $out) { echo $out; } Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446847 Share on other sites More sharing options...
Matic Posted August 26, 2013 Author Share Posted August 26, 2013 Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/281550-display-database-rows-in-html-by-id/#findComment-1446849 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.