Jump to content

Using a query in multiple php sections


perky416

Recommended Posts

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

... 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>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.