chronister Posted March 21, 2007 Share Posted March 21, 2007 I have a dumb question. I think I know the answer. Is it possible to use a variable before actually setting it. I have a page that runs a query and displays the results. I would like to place the Showing 1 to 30 of 150 line in a part of the page that is parsed before the function is actually called. Is there a way to put a var in a page and have it filled in later ? Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/ Share on other sites More sharing options...
vbnullchar Posted March 21, 2007 Share Posted March 21, 2007 i think yes.. @my_var=""; Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/#findComment-211807 Share on other sites More sharing options...
skali Posted March 21, 2007 Share Posted March 21, 2007 You should separate your 'model' from your 'view' then you won't face this problem. All the logic and variables will be set in the model and after that you will parse your views. Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/#findComment-211812 Share on other sites More sharing options...
chronister Posted March 21, 2007 Author Share Posted March 21, 2007 I have a page called header.php... included in this page is a file called includes.php which holds several global vars, a few more includes and such. header.php is called on every one of my pages. I am trying to show the showing part in the top of /movies/index.php The function that displays the page is not called till later on in the page. So what would you suggest? I tried @var=""; and it did not work. Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/#findComment-212135 Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 http://us2.php.net/manual/en/function.ob-start.php Use the buffer output section to grab the contents and than replace X with Y after the variable is defined. Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/#findComment-212139 Share on other sites More sharing options...
chronister Posted March 22, 2007 Author Share Posted March 22, 2007 http://us2.php.net/manual/en/function.ob-start.php Use the buffer output section to grab the contents and than replace X with Y after the variable is defined. Can you explain a little more. I tried ob_start() and ob_end_flush() at the top and bottom of header.php, but that did not work. I looked at ob_get_contents() and that looks like where I need to go with this, but I am not sure how to approach it. I have attached the important pieces of my code to show you where data is coming from and such. I attached a screenshot to show you the movies/index.php page where the showing .... part is displayed. I have 2 spots marked in the screenshot... one is where it is displaying now, and the one above it is where I want it to be displayed. When I move the code there it is out of scope and not defined yet, so it just shows ' -- ' there. header.php -- This file is called at the top of every page. <?php include('etc/includes.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> etc/includes.php -- this file is called in header.php to keep header.php code a little more clean. <?php //code removed I have a couple functions, and a connectdb() function in here as well as several more //variables. include('library_include.php'); include('address_book_include.php'); include('repeat_event_functions.php'); include('movie_include.php'); include('stitch_include.php'); global $message,$color1,$color2,$begin,$end,$total_entries; ?> /movies/index.php (see attached screenshot) <?php include('../header.php'); ?> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top"> <h2>Movie List</h2> <!-- this is where I want to display Showing X - X of XX --> <div class="boxed" ><?php showletters(); ?></div> <?php if(isset($_GET['movie_id'])){ $movie_id=$_GET['movie_id']; get_movie_details($movie_id); }else{ get_movies(); // this where the function that generates the variables is called } ?></td> <td width="175" valign="top" style="padding-left:10px"><h2 >Categories</h2> <div class="boxed"> </div> <span class="categories"><?php show_categories(); ?></span> <br /> <h2>Movie Admin</h2> <div class="boxed"> </div> <a href="/movies/add_movie.php">Add Movie</a><br> <a href="../movies/add_category.php">Categories</a><br> </td> </tr> </table> <?php footer(); ?> movie_include.php <?php ////////////////////////////////////////////////// // movie_include.php // get_movies() // ////////////////////////////////////////////////// function get_movies(){ // code removed // $total entries is the number of rows that was returned in the query $total_entries = mysql_num_rows($result); // set max per page to display $setlimit = 30; //code removed //is $_GET['page'] set, if yes, set $page to it $page = isset($_GET['page']) ? $_GET['page'] : 1; // set the starting number of the results $begin=($offset + 1); // set the ending number of the results $end=($setlimit*$page); ?> <table width="100%" align="center" cellpadding="0" cellspacing="0" border="0"> <tr> <td height="30" colspan="2" align="right" valign="top"> <!-- This is the line I want to move --> Showing <?=$begin ?> - <?php if($end > $total_entries){ echo $total_entries;}else{echo $end;} ?> of <?=$total_entries ?> </td> </tr> <? // A bunch more code has been removed ?> Thanks for your input [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/43617-calling-variables-outside-of-function/#findComment-212568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.