perky416 Posted July 20, 2011 Share Posted July 20, 2011 Hi everyone, Is it possible to use a query in multiple sections of php that are separated by html? If you look at the example below, You can see my defined query at the top of the page, but I also want to use the same query for the while loop that has been put within the html to repeat the textbox. I know I could get around this by putting the html within php however I wanted to keep my html and php separate if possible. <?php $query = mysql_query("SELECT whatever FROM table1 WHERE id='1'"); // some php here ?> <html> <form> <?php while (mysql_fetch_assoc($query){ ?> <input type="text" /> <?php } ?> </form> </html> Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted July 20, 2011 Share Posted July 20, 2011 Not to be dismissive but Have you tried it? Quote Link to comment Share on other sites More sharing options...
perky416 Posted July 20, 2011 Author Share Posted July 20, 2011 Hi requinix, Yes I tried it lol. Its as if its not reading the query. To get it to work I actually have to place a 2nd query exactly the same within the same php tags as the while query. <?php $query = mysql_query("SELECT whatever FROM table1 WHERE id='1'"); while (mysql_fetch_assoc($query){ ?> Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 20, 2011 Share Posted July 20, 2011 Although this is possible, you should really consider keeping your logic (PHP code) and presentation (HTML code) separate. Run all your PHP code to build variables that contain the output, then just echo those variables within the HTML code. It will make your life so much easier in the long run. Interspersing PHP code within HTML code makes the code more difficult to read/understand and is harder to maintain. Quote Link to comment Share on other sites More sharing options...
perky416 Posted July 20, 2011 Author Share Posted July 20, 2011 Hi mjdamato, I completely agree with you, I would love to keep my php and html totally separate! I have a right game trying to interpret it all. The only problem is i dont know how. For example how would I go about displaying a form text box for each row in a database table without using php within the html? Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted July 20, 2011 Share Posted July 20, 2011 You don't have to use $query in the same code block it's defined. There is something else going on: maybe the query is wrong, or you're overwriting $query somewhere? Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 20, 2011 Share Posted July 20, 2011 ... how would I go about displaying a form text box for each row in a database table without using php within the html? As I stated, you use the PHP code to build up the HTML in a variable.Then you only echo the variable in the html code. Here is a very basic example below. You can then separate your logic (PHP) from the output (HTML) into different files. That gives you a log of flexibility - then you can reuse the logic and repurpose it for different displays/output for example <?php //Create and run query $query = "SELECT id, first, last, phone, email FROM users ORDER BY last, first"; $result = mysql_query($query); //Check results and create output if(!$result) { //A DB error occured $tableOutput = "<tr><td colspan=\"5\">There was a problem fetching the records</td></tr>\n" } elseif(mysql_num_rows($result)==0) { //No results returned $tableOutput = "<tr><td colspan=\"5\"No users exist</td></tr>\n" } else { //Process results into HTML $tableOutput = ''; while($row = mysql_fetch_assoc($result)) { //Created vars for full name, edit url and delete url $name = "{$row['last']}, {$row['first']}"; $editURL = "editRecord.php?id={{$row['id']}}"; $deleteURL = "deleteRecord.php?id={{$row['id']}}"; //Create the HTML forthe table record $tableOutput .= "<tr>\n"; $tableOutput .= "<td>{$name}</td>\n"; $tableOutput .= "<td>{$row['phone']}</td>\n"; $tableOutput .= "<td>{$row['email']}</td>\n"; $tableOutput .= "<td><a href=\"{$editURL}\"><Edit</a></td>\n"; $tableOutput .= "<td><a href=\"{$deleteURL}\"><Edit</a></td>\n"; $tableOutput .= "</tr>\n"; } } ?> <html> <body> <h1>User List</h1> <table> <tr> <th>Name</th> <th>Phone</th> <th>Email</th> <th>Edit</th> <th>Delete</th> </tr> <?php echo $tableHTML; ?> </table> </body> </html> 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.