Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Variable From Line Further Then Current Line?
bakr_2k5
post Nov 18 2006, 02:13 PM
Post #1


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Hello,

Is it in some way possible to load a variable that further then the current line in the script?
I don't know if you know what i mean so I'll try to point it out in the script below.

CODE

1 <?php
2      echo $var1;
3      # $var1 needs to be loaded from foo.php
4     # Lets say i want $var1 to be echoed in <title>$var1</title>
5       - - - -
      bla bla bla
55      - - - -
56      switch($_GET['page']) {
57          case "foo":
58              include("foo.php");
59              #- - - - from foo.php - - - -
60                   $var1 = "what ever you want";
61              #- - - - end from foo.php - - - -
62              break;
63      }
64 ?>

Well is it possible at all?
I already tried with "ob_start() / ob_end_flush()" but doesn't work either... (understood the purpose of ob_start() a bit wrong rolleyes.gif)
If you know a solution or an other way doing this, please tell me smile.gif

bakr_2k5
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 18 2006, 03:01 PM
Post #2


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



QUOTE(bakr_2k5 @ Nov 18 2006, 10:13 PM) *

CODE

1 <?php
2      echo $var1;
3      # $var1 needs to be loaded from foo.php
4     # Lets say i want $var1 to be echoed in <title>$var1</title>
5       - - - -
      bla bla bla
55      - - - -
56      switch($_GET['page']) {
57          case "foo":
58              include("foo.php");
59              #- - - - from foo.php - - - -
60                   $var1 = "what ever you want";
61              #- - - - end from foo.php - - - -
62              break;
63      }
64 ?>


Maybe you can move the whole section from line 56 onwards to before "title", or in other words, delay the output of title after line 64. Whatever that's between line 5 to 56, if it's output of html, can be easily move to after line 56.

Another way to prevent such problem next time is to use template engine, or something similiar. I've copied some tactic from some common php template engine and wrote my own one, though less powerful, and not optimize. I split the php and the html. Say if this page is mypage.html, i took out the php code, put inside mypage.php. Replace anything i need to output in the html with {VAR_NAME}, towards the end of the php file, i just read the html into memory, then use str_replace to put in the variable. Then your problem will not appear at all. E.g.
CODE

HTML :
<title>{TITLE}</title>

PHP:
str_replace("{TITLE}", $var1, $page);


After that just print out the html in memory. Another advantage of this method is you don't have to repeatedly add section on the page that appear on every other page.

Say you need to print the username at the top of the page. What i did was place the last print out function into another php file, change it into a function, then add
CODE


str_replace("{USERNAME}, $username, $page);

OR

str_replace("{USERNAME}, $_SESSION['username'], $page);



before the print out function, that's all, then on those page that i need to display the username, just put in {USERNAME}. Provided you've set the $username or session properly

If you suddenly see {SOMETHING}, then you know you've miss something. Or you can place {ANYTHING} at those places you want to put something there, but will only do later, that way you won't forgot to do it.

Another benefit of this method is that you can pass the html to a designer to touch up your page, while they don't have to worry that thet'll break you php codes. At the same time, you can still update your php code without waiting for them.
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 18 2006, 03:21 PM
Post #3


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Hi faulty.lee,

That's not exactly what i meant,
That switch from line 56 to 63 puts it's content in to a table
-------------------------
| top |
-------------------------
| m | |
| e | |
| n | content |
| u | |
--------------------------
EDIT:
Right ... The ASCII thing doesn't really work the way i want!
END EDIT!
My main page is build up with tables and everything goes from there.
In the content section lays that switch from line 56 to 63, so it loads the page it needs
into the content table.
CODE

<?php
switch($_GET['page'])
  {
  case '1' : include('page1.php'); break;
  case '2' : include('page2.php'); break;
  case '3' : include('page3.php'); break;
  # etc.
  }
?>

That script is something like mine in the content table. So i can't really move lines 56 to 63 to the top (or some where near) in the script, because the page will be included at the top what's not the purpose of it wink.gif

I hope you understand what I mean, I'm from the Netherlands so it could be weird for English people to understand it wink.gif

bakr_2k5

This post has been edited by bakr_2k5: Nov 18 2006, 03:27 PM
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 18 2006, 03:44 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



From the "english" point of view, that makes 2 of us, i'm not english, either is my mother language. I'm chinese

In that case, i don't think it's possible. Well i might be wrong. So if you use "ob_start() / ob_end_flush()", you'll have to manipulate the output buffer, which i believe would not worth the trouble.

As a general programming guideline, if you program need to do some tweak or something ppl seldom do, you show rethink your design, as there should be a better proven method. (there's an original cooler way of saying this, but i forgot how was it tongue.gif )

In that case, i'll recommend you to try the template method, Won't take too long to convert, well unless you have thousand of files, and in that case you should have been using template in the first place. If you want to try my so call engine, let me know, i can post it here.
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 18 2006, 04:02 PM
Post #5


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Well it isn't THAT important wink.gif
But it would be nice if it was possible.

If you want to, i would like to try your engine. Maybe I'll get some ideas from it to solve my problem. laugh.gif

Thank you so far smile.gif

bakr_2k5
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 18 2006, 04:48 PM
Post #6


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



Not really an engine per se, but didn't know better words. Use at your own risk, and it's not optimized
This is the part in the common file
CODE

function template_open($filename)
{
    if(!file_exists($filename))
    {
        return false;
    }
    $handle = fopen($filename, "r");
    $content = "";
    while (!feof($handle)){[code]
        $content = $content . fread($handle, 8192);
    }
    fclose($handle);
    return $content;
}

function output_page(&$page)
{
    global $username;

    $page = str_replace("{USERNAME}", $username, $page);
    echo $page;
}


This part in the page itself "./index.php"
CODE

$temp_main = "./templates/index.html";
$temp_menu = "./templates/menu.html";

$menu = template_open($temp_menu);
$page = template_open($temp_main);
if ($page)
{
    if ($menu)
    {
        $page = str_replace("{CONTENT}", $content, $page);

        //This one load the "menu" page into "{MENU}" section.
        //More or less like the "include('page1.php')"
        $page = str_replace("{MENU}", $menu, $page);
        output_page();
    }
}


If you need to have processing on the"menu.php", then you can encapsulate it into a function which return the processed HTML as $menu.

If you can follow up till here. Then the next part is much more powerful one, it's to generate table with odd or even row. Cause i use diff coloring for each that's why.

This part inside common
CODE

function template2_open($filename, &$row_content_odd, &$row_content_even){
    if(!file_exists($filename))
    {
        return false;
    }
    $handle = fopen($filename, "r");
    $content = "";
    while (!feof($handle)){
        $content = $content . fread($handle, 8192);
    }
    fclose($handle);
    //$content2 = explode("\n", $content);
    
    $start_odd_row_pos = strpos($content, "BEGIN_ODD_ROW");
    $start_odd_row_pos = strpos($content, "\n", $start_odd_row_pos);
    $end_odd_row_pos = strpos($content, "END_ODD_ROW", $start_odd_row_pos);
    $end_odd_row_pos = strpos($content, "\n", $end_odd_row_pos);
    $start_even_row_pos = strpos($content, "BEGIN_EVEN_ROW");
    $start_even_row_pos = strpos($content, "\n", $start_even_row_pos);
    $end_even_row_pos = strpos($content, "END_EVEN_ROW", $start_even_row_pos);
    $end_even_row_pos = strpos($content, "\n", $end_even_row_pos);

    $row_content_odd = substr($content, $start_odd_row_pos, $end_odd_row_pos - $start_odd_row_pos);
    $row_content_even = substr($content, $start_even_row_pos, $end_even_row_pos - $start_even_row_pos);
    $content = substr_replace($content, "{ROW_CONTENT}", $start_odd_row_pos, $end_even_row_pos - $start_odd_row_pos);
    return $content;
}


The part of HTML, i only show the table part
the 4 tag has to be in new line each
CODE
<!-- BEGIN_ODD_ROW -->
<!-- END_ODD_ROW -->
<!-- BEGIN_EVEN_ROW -->
<!-- END_EVEN_ROW -->

CODE

<table>
<!-- BEGIN_ODD_ROW -->
    <tr>
          <td>{COUNTER}</td>
          <td>This is odd row</td>
          <td>{DATA}</td>
    </tr>
<!-- END_ODD_ROW -->
<!-- BEGIN_EVEN_ROW -->
    <tr>
          <td>{COUNTER}</td>
          <td>This is even row</td>
          <td>{DATA}</td>
    </tr>
<!-- END_EVEN_ROW -->


This is the part in the page.
CODE

$temp_main = "./templates/index.html";
$temp_table = "./templates/table.html";

//Use diff function here. and you need to declare the odd and even row first
$row_content_odd = "";
$row_content_even = "";
$row_content = "";
$table= template2_open($temp_table, $row_content_odd, $row_content_even);

$page = template_open($temp_main);

if ($page)
{
    if ($table)
    {
        $page = str_replace("{CONTENT}", $content, $page);

        $row_counter = 0;
        for ($row_counter = 0; $counter < 10; $counter++)
        {
            if ($row_counter % 2 == 0)
            {
                $row_content_temp = $row_content_odd;
            }
            else
            {
                $row_content_temp = $row_content_even;
            }
            $row_content_temp = str_replace("{COUNTER}", $counter, $row_content_temp);
            $row_content_temp = str_replace("{DATA}", "Some data", $row_content_temp);

            $row_content .= $row_content_temp;
            $row_counter++;

        }
        
        //"{ROW_CONTENT}" this one you can't change, as it is inserted by template2_open
        $menu = str_replace("{ROW_CONTENT}", $row_content, $menu);
        $page = str_replace("{MENU}", $table, $page);
        output_page();
    }
}



That's it, if you happen to improve this, and if you don't can, please share with me or us, the fellow forumer

Good luck.
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 19 2006, 09:14 AM
Post #7


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Thank you faulty.lee,

I'll take a look at it and see if it's usable in my scripts. It seems a bit complicated.
So need to figure out what everything does. But it looks like something very useful! wink.gif

bakr_2k5
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 19 2006, 09:33 AM
Post #8


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



Sorry, i was very lazy at putting in comment. Anyway, feel free to ask if you don't understand. Or just change the coding around to see how it works.

That code has works for me for quite sometime, save a lot of hours of programming also. Glad it helps.
Go to the top of the page
 
+Quote Post
CrazyPensil
post Nov 19 2006, 02:44 PM
Post #9


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 42
Joined: 17-March 06
From: Russia, St.Petersburg
Member No.: 12,058



CODE
<html>
<head>
<?php      
      switch($_GET['page']) {
          case "foo":
             include("foo.php");
              #- - - - from foo.php - - - -
                  $var1 = "what ever you want";
             #- - - - end from foo.php - - - -
              break;
     }
     echo "<title>$var1</title>;
?>


This post has been edited by CrazyPensil: Nov 19 2006, 02:45 PM
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 20 2006, 02:07 PM
Post #10


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(CrazyPensil @ Nov 19 2006, 02:44 PM) *

CODE
<html>
<head>
<?php      
      switch($_GET['page']) {
          case "foo":
             include("foo.php");
              #- - - - from foo.php - - - -
                  $var1 = "what ever you want";
             #- - - - end from foo.php - - - -
              break;
     }
     echo "<title>$var1</title>;
?>



This won't work ... foo.php is for example news.php it contains < $var1 = "news" >
Then <title>*Sitename* || <?php echo $var1 ?></title>...
So your code wouldn't work ... But it isn't possible the way i want.

Thanks for trying anyways.

Bakr_2k5
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Reset The Server Variable Php_auth_user(9)
  2. Quickly Create Form Variables(5)
  3. Php Any Variable In String.(1)
  4. Unexpected Error(2)
  5. Send Php Variable To Javascript(5)
  6. Permanent Variable(7)