-2

I'm trying to avoid to query my database twice: for set <title> attribute and also for echo the page title. I want to query it just one time:

Example:

<html>
  <head>
  <?php
    // how should I use here ob_start() ? Is there any other possible way to achieve this?
    $title = "";
    echo '<title>', $title, '</title>'; // this should be in the end <title>value of $row['page_title']</title>
  ?>
  </head>
  <body>
     <?php
       $sql = $mysqli->query("MY QUERY");
       $row = $sql->fetch_assoc();
       $title = $row['page_title']; // I want that this assignment to set the variable in the top
       // I know that for this job I can use ob_start() but I didn't used it until now
       // and I will really appreciate any help from you.

     ?>
     <h1><?php echo $title; ?></h1>
  </body>
</html>

I know that I can do the query before echo the title attribute but I don't want to do it like that. Do you have any suggestion? or can you show me how to use that ob_start() / ob_clean() functions?

Thank you!

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Why is it that you don't want to do the query at the start and then just reference the variables later? – qooplmao Dec 05 '12 at 13:27
  • Because I'm including files dynamically depending on URL. The `

    ` in the example above is in many other files..

    – Mihai Matei Dec 05 '12 at 13:29
  • If the h1 is coming from an include file my answer still stands, as long as the file is included after the block of code being shifted above – Dale Dec 05 '12 at 13:36
  • The example above is not my case. I'm developing an ecommerce platform and I will have different queries for each page.. If there is a product page I should print the product name, if there is help page then I should print the question, etc.. I don't want to make all queries in that place.. Even if I can verify which page is called I want that each code to be placed in the same file not in separate files. – Mihai Matei Dec 05 '12 at 13:40
  • And you decided to leave all that out of your question because? – Dale Dec 05 '12 at 13:51
  • Because that is not relevant.. I've wrote in my question that I know the possibility to do like you answered and is not the solution I want!!! – Mihai Matei Dec 05 '12 at 13:55
  • It's totally relevant and it caused me to waste my time trying to help you without knowing you're creating a total mess of an e-commerce system, the one last thing I will suggest to you is to go with a tried and tested MVC approach and not reinvent the wheel – Dale Dec 05 '12 at 13:58

2 Answers2

1

Shift the query to the top of the code and reuse the variables!

<?php
       $sql = $mysqli->query("MY QUERY");
       $row = $sql->fetch_assoc();
       $title = $row['page_title']; 
?>
<html>
  <head>
  <?php echo '<title>', $title, '</title>'; ?>
  </head>
  <body>
     <h1><?php echo $title; ?></h1>
  </body>
</html>
Dale
  • 10,384
  • 21
  • 34
  • I wrote in my question content that I know I can do it like this but I don't want this method. thank you for your answer – Mihai Matei Dec 05 '12 at 13:30
  • Why not? It’s the conventional way of writing a page that includes both PHP and HTML in a procedural fashion like yours. You don’t have to go trailing through the HTML to find where your PHP starts; your variables are already set to be used. You’re arguing against years of experience and learning by millions or developers by refusing to lay out your page’s code in this way. – Martin Bean Dec 05 '12 at 13:54
  • @MateiMihai this is the best way to handle your problem. You should find a way to do your dynamic includes within this model, and not the other way round. – didierc Dec 05 '12 at 13:56
0

ob_start();, in usual sense, won't help you have. However, you can use ugly solution like this:

ob_start();
echo '
  <html>
    <head>
      <title>{title}</title>
    </head>
  <body>
    ';
$header = ob_get_clean();

// and then, when you know your title
echo str_replace('{title}', $known_title, $header);

But I strongly recommend taking another approach. You need to choose and fill the template after all data has been gathered and you know all details about which template you want. If you start echoing different parts preemptively, you will get more and more troubles. Now you need to change title for some pages. Then you will need to add css file for specific page and you will have to do that ugly business again. Why not do it the right way?

Ranty
  • 3,333
  • 3
  • 22
  • 24