Jump to content


Photo

PHP variables populate to HTML


  • Please log in to reply
6 replies to this topic

#1 cjbeck71081

cjbeck71081
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 30 August 2006 - 04:21 PM

I am a new to PHP and i think my question is basic, but i am having no luck finding the answer.

I have an sql database on my website with data populate for fields UserID, FirstName, LastName, and Email

I created an HTML page called "userid.html" with this code:


[font=Verdana][font=Verdana]</head>

<body>

<form method="post" action="http://epicri.com/info.php">


<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</p>
<form id="form1" name="form1" method="post" action="">
  <label>
  <div align="center"><span class="style1">Please Enter User ID: </span><br />
    <br />
    <input name="userid" type="text" size="10" maxlength="5" />
    <br />
    <br />
    <input type="submit" name="Go" value="Submit" />
  </div>
  </label>
</form>
<p align="center">&nbsp;</p>
</body>
</html>


that sent the value of the USERID entered into a textbox to a PHP script named "info.php" that then query'd the database for the data that looked like this:

</head>

<body>

<?php
$hostname = "mysql.epicri.com";
$username = "epicr001";
$password = "xxxxx";
$usertable = "persontab";
$dbName = "epicr001"; //
$word = $_POST["userid"]


?>

<?php
$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbName, $con);

$result = mysql_query("SELECT firstname,lastname,email,userid FROM $usertable
WHERE userid='$word'");

while($row = mysql_fetch_array($result))

  {
  echo $row['firstname'] . " " . $row['lastname'];
  echo "<br />";
  }


?>

</body>



What i would like to do is now populate an HTML page with the results of the Query without using the "echo" code. For example after the information is sent to the PHP the resulting HTML page would look like:


John Smith
UserID: 12221
First Name: John
Last Name: Smith
Email Address: john@epicri.com

I am almost sure my problem is the syntaxt of passing PHP Variables for use in an HTML form, but i dont know

Any help is apprecaited
Thank You
Chris




#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 04:29 PM

Without echo nothing will be outputted. So you'll need to use echo to generate the html. or do you ant to create an HTML file with the output of the query?

#3 cjbeck71081

cjbeck71081
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 30 August 2006 - 04:37 PM

I want to make an HTML file from the output of the query

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 04:51 PM

Okay use this:
$htmlCode = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Unititled Document</title>
</head>
<body>

HTML;

$row = mysql_fetch_array($result);

$name = $row['firstname'] . '_' . $row['lastname'];

$htmlCode .= <<<HTML
<b>{$name}</b><br />
<b>User ID:</b> {$row['userid']}<br />
<b>First Name:</b> {$row['firstname']}<br />
<b>Last Name:</b> {$row['Lastname']}<br />
<b>Email Address:</b> {$row['email']}

</body>
</html>
HTML;

// create the HTML File
// the html file will be name:
// fistname_lastname.html
$htmlFile = fopen($name . '.html', "w+");

// write to the html file
fwrite($htmlFile, $htmlCode, strlen($htmlCode));

// now we close the file
fclose($htmlFile);

echo "HTML file created!<br />\n<br />\n<a href=\"{$name}.html\">{$name}.html</a>";

instead of the following:
while($row = mysql_fetch_array($result))

  {
  echo $row['firstname'] . " " . $row['lastname'];
  echo "
";
  }

NOTE: This code is untested, so it may return errors.

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 August 2006 - 04:55 PM

You can write any html code you like. When it comes to to put something in that comes from the database you can use this

<?=$row['firstname']?>

So in html it will look like this

<td class=someclass align=center>First Name: <?=$row['firstname']?></td>

or the other way around. start your table above your while statement then echo out the html code with the database results. You should start the table above the while statement that way you can loop thru each row starting with <tr> otherwise you will start a new table on every loop
echo "<table>"
while($row = mysql_fetch_array($result))  {
  echo "<tr>
<td>".$row['firstname']."</td><td>" . $row['lastname']."</td>
</tr>";
  }
echo "</table>"

It that what you are looking for???

Ray

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 05:02 PM

He wants to create a HTML file from the output generated from the query. He doesnt want to display results from the script that runs the query. Which is what my code above does.

#7 cjbeck71081

cjbeck71081
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 30 August 2006 - 05:10 PM

Thank you both for your assistance.

The output from query worked great!.

Ultimately what i was looking for however was the second answer regarding the <td><?=$row['firstname']?></td> code.

I realise I made a mistake in how i asked the question, but both responses were a huge help.

I have been reading information on code for some 100 hours in the last 2 weeks ultimately looking for this information... i shoudlve came here in the first place.

Thank You again guys
Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users