sazzie Posted January 12, 2007 Share Posted January 12, 2007 How do you force a piece of php code to be called only once?My attempts at useing require_once fails as the calling script is used iteratively and the required_once script gets loaded again and again.Please help. Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/ Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 [quote author=sazzie link=topic=122083.msg503024#msg503024 date=1168604470]How do you force a piece of php code to be called only once?[/quote]Only call it once :)How do you know it's being requested more than once?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159018 Share on other sites More sharing options...
sazzie Posted January 12, 2007 Author Share Posted January 12, 2007 Because the file being required_once is a php generated tabs setup.Each time the require script runs, and it does so iteratively( acting differently each time with different input) , a new set oftabs are drawn up overlapping the former one(s). Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159023 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 In that case you've misunderstood what require_once() actually does...Imagine this:[b]index.php[/b][code]<?phprequire('connect.php');require('header.php');// Rest of the page code here...?>[/code][b]header.php[/b][code]<?phprequire('connect.php');// Rest of the page code here...?>[/code]You can see here that connect.php is going to be called and loaded twice, once by index and once when header.php is called by index. A 12k page has just become 24k of bandwidth. But more importantly, you'll avoid variable redefinitions.By using require_once('connect.php') it will fetch/retrieve this page and if it comes across a require('connect.php') again, it won't attempt to retrieve it, knowing full well that it has it already. Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159026 Share on other sites More sharing options...
sazzie Posted January 12, 2007 Author Share Posted January 12, 2007 Are you saying if A.php has a call to require_once B.php but A.php calls it's self repeatedly, there will be multipleinclusions of B.php?If the above is right, can you suggest a way out? Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159027 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 [quote author=sazzie link=topic=122083.msg503037#msg503037 date=1168605560]can you suggest a way out?[/quote]If you're able to provide some code then we could look into this for you. Don't forget to surround your code with [nobbc][code]...[/code][/nobbc] tags :)RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159031 Share on other sites More sharing options...
sazzie Posted January 12, 2007 Author Share Posted January 12, 2007 Here is the code.This is A.php :[code]<?php[color=red] require_once("B.php"); if( isset($_GET['task']) ) { $task = $_GET['task']; switch( $task ) { case 0: courses_welcome(); break; case 1: t1(); break; case 2: t2(); break; case 3: t3(); break; default : break; } } function courses_welcome() { echo "<p>Welcome to our courses</p>"; } function t1() { echo "Testing 1!"; } function t2() { echo "Testing 2!"; } function t3() { echo "Testing 3!"; }[/color]?>[/code]This is B.php :[code]<?php{[color=red] echo "<div>"; echo "<div class=\"tabArea\">"; echo "<a class=\"tab\" href=\"#\" onClick=\"tab_manager('tabContent', 'A.php?task=0');\"> Welcome </a>"; echo "<a class=\"tab\" href=\"#\" onClick=\"tab_manager('tabContent', 'A.php?task=1');\"> View All Courses </a>"; echo "<a class=\"tab\" href=\"#\" onClick=\"tab_manager('tabContent', 'A.php?task=2');\"> View '07 Courses </a>"; echo "<a class=\"tab\" href=\"#\" onClick=\"tab_manager('tabContent', 'A.php?task=3');\"> Add Courses </a>"; echo "</div>"; echo "<div class=\"tabMain\">"; echo "<div id=\"tabIframeWrapper\">"; echo "<div id=\"tabContent\"></div>"; echo "</div>"; echo "</div>";}?>[/color][/code]And the styling just incase : [code]div.tabArea { font-size: 80%; font-weight: bold; border: solid 1px blue; } a.tab { background-color: #f0f0f0; border: 1px solid #000000; border-bottom-width: 0px; padding: 2px 1em 2px 1em; position: relative; text-decoration: none; top: -1px; z-index: 100; } a.tab, a.tab:visited { color: #808080; } a.tab:hover { background-color: #d0d0d0; color: #606060; } a.tab.activeTab, a.tab.activeTab:hover, a.tab.activeTab:visited { background-color: #c0c0c0; color: #000000; } a.tab.activeTab { z-index: 102; } div.tabMain { background-color: #c0c0c0; border: 1px solid #000000; padding: 1em; position: relative; z-index: 101; } #tabIframeWrapper { width: 100%; } #tabContent { border: 1px solid green; padding: 0px 15px 15px 15px; width: 100%; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159112 Share on other sites More sharing options...
sazzie Posted January 12, 2007 Author Share Posted January 12, 2007 I forgot to mention the code is part of a larger Ajax project.So because I have had to cut out irrelivant code the code provided probably won't duplicate the error I described but it will show the concept of how me code interacts with regards to the required_once call. Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159134 Share on other sites More sharing options...
HuggieBear Posted January 12, 2007 Share Posted January 12, 2007 You're right, I'm unable to recreate the problem. Looking at what you have, I can see what you mean, but I can't see how it's likely to cause a problem. Maybe your javascript 'tab_manager' function is at fault.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-159356 Share on other sites More sharing options...
sazzie Posted January 15, 2007 Author Share Posted January 15, 2007 Thanks Huggie'. Sorry for the time away - weekend, you see!.The only other tricks I have up my sleeves right now are :1. To remove the former rendering of the divs tab and then over write with a new one which should sit in the right place - fingers and toes crossed.2. To render the tabs in a script which calls only once. So the script before the iterative one( A.php ).Do you know code for 'erasing' and entire div from DOM to help with the first approach? I'm sorry for asking here. I amsure it's not the right place. :D Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-160989 Share on other sites More sharing options...
HuggieBear Posted January 15, 2007 Share Posted January 15, 2007 I'm afraid I don't but try posting in the [url=http://www.phpfreaks.com/forums/index.php/board,6.0.html]Javascript forum[/url] that we have here at PHPFreaksHuggie Quote Link to comment https://forums.phpfreaks.com/topic/33881-require-once-issues/#findComment-160993 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.