niko
niko

Reputation: 9393

best approach in filling up a profile page?

Well I am a newbee so kindly please ignore if there is any mistake in my question,I have this question on my mind.Iam just creating a small website like a profile information.

Okay see when user logs in with his email into the website, we use php scripting and retrieve the data from mysql and put that data in the page using mostly printf or echo

So there is possibilty of 2 ways to do

for example one of the table of my database looks like this

   name        birthday aboutme gender dreams music movies blabla//
   someperson   xx        xx      xx     xx     xx   xx

first method demo.php

 <html>
 <body>
 name: <?some php script to get user name?>
 <20 lines of html tags and data>
 birthday: <?some php script to get user name?>
 <20 lines of html tags and data>
 about user <?some php script to get user about?>
 <some more html data>
 <?some more php scripting?>

In the first method we are always connecting to the database and getting the data and then after 20 lines of html code and then again we are connecting to the same table and database and getting the next columns value and disconnecting it and then again we are connecting to the server and more. In this process we are connecting and disconnecting from server

So in the first method we are always connecting and disconnecting from server, I was just thinking it may cause a serious overhead on the server as we are connecting and disconnecting for every 10 lines of code.is my thinking true or is there anything wrong?

second method demo.php

 <?php
 retrieve all the data you need at a time here and then use 
 echo "<html>"
 echo "<body>"
 echo "name: <?just php variable value that was retrieved?>
 echo "<20 lines of html tags and data>"
 echo "birthday: <?just php variable value that was retrieved?>
 ?>

In the second method we are using the php script run by server just get the data at the start and then just echo them with the html tags and data that was retrieved, but i was thinking it is so unneccasary because we are making the server to execute the html tags where a normal browser clearly would do

I am finding both ways causing a serious overhead issue on the server.could anyone explain what is the best way to do it? is there any other way the professionals choose , I am not sure how they make through it.

Well is there a way to retrieve the data at once from the database and then just echo it back between the html tags?

simply like

  <?get all the data from database here>? 
  <div> echo 'some php variable'
  <span> echo 'some php variable'

Is there a way like this? Kindly let me know the best approach to do it.Thanks

any help is greatly appreciated

Upvotes: 2

Views: 122

Answers (3)

VictorKilo
VictorKilo

Reputation: 1870

You are correct in thinking that you do not have to declare all of your html using php echo. The php generates the HTML as it moves down your code. So you can declare HTML inbetween php calls.

For example:

<html>
<body>
<?php 
  $results = mysql_query(SELECT name,gender FROM table WHERE id='34';
  // Additional code to assign $results array to variables
?>
<div><?=$name;?></div>
<div><?=$age;?></div>
</body>
</html>

The most important thing to note is that you can call php multiple times through out your document and it will continue to work off past php calls. All variables and functions are stored throughout the page.

Hope this helps.

Upvotes: 1

jcmeloni
jcmeloni

Reputation: 1234

Yes, I would get all of the data up front if that is possible (if it is not dependent on anything else in the execution of your script).

If I follow what you are saying correctly, you are pretty close in your pseudocode at the end there. You seem to want something like this:

<? 
// here is where you get all the data from MySQL using PHP
?>
<!-- HTML now -->
<div>
<?
//PHP starts again
echo $something;
//PHP ends
?>
<!-- HTML again -->
</div>

You can jump in and out of PHP as many times as you'd like, whenever is most appropriate.

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

If all your data is in the same table then you only need a single query. And you should switch in and out of php instead of composing the html inside php. It might look something like this:

<?php

$db = new PDO($dsn, $user, $password);
$stmt = $db->prepare('SELECT * FROM user_profile WHERE user_id = ?');
$stmt->execute(array($user_id));
$profile = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->close();

?>
<html>
 <head>
   <title> <?php echo $profile['username']; ?> Profile</title>
 </head>
 <body>
   <div>
    <div>
     <div>
      <?php foreach($profile as $attribute => $value): ?>
        <div>
          <span class="label"><?php echo $attribute ?></span> 
          <span class="profile_value"><?php echo $value; ?> </span>
        </div>
      <?php endforeach; ?>
     </div>
    </div>
   </div>
 </body>
</html>

Upvotes: 1

Related Questions