PHP - Fairly Basic Question - Issue With An Include And An Argument That Needs Passing
I think this is a simple question, but I'm just starting out and can't figure out how to do this exactly.
I'm making a demo site for a flower shop. The structure is as such (only listing the particular files in question - once this is done, the rest will be easy) index.php -> inc_show_colors.php -> showColors.php The index is to be standard through the site, loading include files into a main content area. Clicking on a link loads inc_by_color.php (index.php -> index.php?page=by_color ) From there, one of the four colors is selected. showColors.php contains switch statements for each color. I want to load showColors.php into the main content window when a color is selected, and I need to have it work for all four colors. I'm not sure how to do this, as you can't pass a variable to an include from what I understand. What's the easiest way to go about this? Keep in mind this is an intro class and "best practices" are not necessary yet, just the simplest solution to get it working. I want each of these colors to link to another include file, which will load in its place in index.php. index.php: Code: [Select] <div id="maincontent"> <?php if (isset($_GET['page'])) { switch ($_GET['page']) { case 'by_color': include('includes/inc_by_color.php'); break; case 'by_type': include('includes/inc_by_type.php'); break; case 'by_occasion': include('includes/inc_by_occasion.php'); break; case 'view_cart': include('includes/inc_view_cart.php'); break; case 'payment_options': include('includes/inc_payment_options.php'); break; case 'home_page': default: include('includes/inc_home.php'); break; } } else include('includes/inc_home.php'); ?> </div> inc_by_color.php: The first link I tried doing in the way I thought would work, but it doesn't. It can't find the include file. The second one works, kind of - but it just loads the page by itself, not into the maincontent div in index.php. Code: [Select] <h1>Flowers Categorized by Color</h1> <div class="picdivcontainer"> <div class="picdiv"><a href="index.php?page=red"><img src="images/image.png" width="90" height="90" alt="Red Flowers" /> <p>Red</a></p></div> <div class="picdiv"><a href="showColors.php?color=white"><img src="images/image.png" width="90" height="90" alt="White Flowers" /></a> <p><a href="showColors.php?color=white">White</a></p></div> </div> and showColors.php: Code: [Select] if (isset($_GET['color'])) { switch ($_GET['color']) { case 'red': $result = mysql_query("SELECT * FROM flowers WHERE color='red'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { // Set and trim strings and show the resulting flowers from query $flowername = $row['name']; $flowerimage = $row['image']; $image_path = "images/"; $flowername = str_replace('_', ' ', $flowername); $flowername = ucwords($flowername); ?> <div class="picdiv"><p><?php echo $flowername."<br />"; $flowerimage = str_replace(' ', '_', $flowerimage); echo "<img src='images/" . $row['image'] . "' />"; ?></p></div> <?php } break; I realize that showColors.php is probably a really messy way of doing this, but I just need to get it functioning ASAP for now. Similar TutorialsI just want to know how to create a global variable from within a function. The reason I want to do this is I'm making mysql queries that may or may not have data in a function, if the data does indeed exist, I want to declare variables at that point. My queries are grouped logically by year/month, and as a result I'm going to be appending data to existing variables if they exist so it makes more sense to just append it to what would be the global variable anyways instead of just passing large strings out of the function to just be appended anyways -- plus it prevents me from creating a bunch of pointless variables. I intend additionally take the variables created inside the function and then store them in an encompassing global array (pre-defined outside of function) and at the end of the script do a foreach through it so I can iterate through the variables to grab my data knowing that there won't be pointless crap in there. I'm trying to create global variables by using variable variables ($$whichever). The code giving me issues inside the function is just like this: Code: [Select] function SeekAndAppend ($vars, $being, $passed) { global $$whatever; // Trying to define it here global $array; // Calling predefined $array $array[] = "$$whatever"; // Passing just created global to the array for iteration later } When I iterate through the array the expected name of what would be the global variable name is there, but the global variable itself does not exist -- just at the function level. If someone has a recommendation on a better way to do it, I'll listen, but please don't turn it into a lecture. EDIT: Grammar & Clarity Hi Guys. This should- hopefully- be a pretty simple question for someone with regex experience to answer. I've been reading all the regex guides and tried various things but none have worked and I'm starting to get a headache. I've decided to just pitch the question here. How do I test if a string contains ONLY a certain character- but could be one or more instances of that character. In this case it's a comma. So: ',' -> Would return TRUE ',,,' -> Would return TRUE ',,,,,,,' -> Would return TRUE ',,,hello,' -> Would return FALSE 'hi,,,' -> Would return FALSE Thanks in advance! Firstly i am new to php.Iv currently got this while loop iterating through a database drawing polygons from the info in the datatable.each polygon has its own id stored in the datatable and with the on click event i am trying to just output to the screen the specific id of the polygon clicked on.It looks ok to me but it doesnt work for some reason. Below is the loop and the function it is trying to call. /////////////////////////////////////////////////////////////////////// <?php while($info = mysql_fetch_array( $data )){ echo "<polygon fill=".$info['fill_colour']." stroke=\"black\" id=".$info['id']." onclick=\"buildingClick(id)\" points=".$info['coordinates']." />"; } ?> ///////////THIS IS THE FUNCTION/////////////////////////// <?php function buildingClick($id) { echo "building id : {$id}"; } ?> /////////////////////////////////////////////////////////////////// If anyone could help it would be hugely appreciated. Eoin hi there, i am fairly new to OOPs in php, i get an error when i declare the argument type (as object) in a function and pass the same type (object). class eBlast { public static function getEmail(object $result) { return $result->email; } } $r = mysql_fetch_object($query); eBlast::getEmail($r); echo gettype($r); // outputs: object error is : Code: [Select] Catchable fatal error: Argument 1 passed to eBlast::getEmail() must be an instance of object, instance of stdClass given, called in C:\wamp\www\integra\client\pl_eblast\admin\send_emails.php on line 145 and defined in C:\wamp\www\integra\client\pl_eblast\app\app.eBlast.php on line 8 if i remove the type declaration in the function it works, but just would like to know why it shows error when pass the same type, also isnt mysql_fetch_object is the instance of stdclass? thanks in advance! I am trying to pass a variable from inside of a function in a file that is included eg include.php Code: [Select] <? function noms(){ $foobar = "apples"; } ?> main.php Code: [Select] <? include("include.php"); noms(); echo "these ". $foobar. " are most delicious... OM NOM NOM"; ?> I am essentially using a include file for a mysql connection and based on the connection outcome i am either setting a value to true or false. It prints "Connected succesfully" and what not just fine, but, after that it wont pass on the variables data. I know its the function because, it passes the data if i put the variable before the function... i just dont get it.. any help would be great. Thanks. Ansel I am quite new to php and the mvc setup, I am developing a library app however starting at the very basics so as not to become overwhelmed!
I am trying to do a basic insert to my book table, this is the code I have so far alsong with the error I am presented with.
Model (models > adminarea_model.php)
adminarea_model.php
public function create($title_text) { $title_text = strip_tags($title_text); $sql = "INSERT INTO book (title) VALUES (:title)"; $query = $this->db->prepare($sql); $query->execute(array(':title' => $title_text)); $count = $query->rowCount(); if ($count == 1) { return true; } else { $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED; } return false; } Hey if you could help me I would really appreciate it, I haven't been coding for a while and I'm getting stuck on quite a basic part of my script, in fact it was the first part. I'm using the include(); function to include a file which will be used to show multiple pages through the $_GET variable, for example if you don't understand what I'm talking about if I was making an "about" page I might do index.php?page=about and it would retrieve the "about" page. Unfortunately I keep getting this error: Quote Warning: include(back_func.php?id=new) [function.include]: failed to open stream: No error in C:\Program Files\EasyPHP-5.3.9\www\link_extension\index.php on line 25 Warning: include() [function.include]: Failed opening 'back_func.php?id=new' for inclusion (include_path='.;C:\php\pear') in C:\Program Files\EasyPHP-5.3.9\www\link_extension\index.php on line 25 Notice: Undefined variable: var in C:\Program Files\EasyPHP-5.3.9\www\link_extension\index.php on line 26 This is the PHP in my index page, notice it consists of one line because I removed the rest for testing: Code: [Select] include('back_func.php?id=new'); echo $var;This is my back_func.php page, also simplified for testing: Code: [Select] <?php if (isset($_GET['id'])) $var = 'worked'; else $var = 'not'; ?> If you don't understand what I'm asking but feel you could help I'm happy to further explain, I'm expecting it's something basic I've forgotten. Thanks. Does it matter where you start your session at? Does it have to be before the <!doctype> tag or can it go anywhere in the page? Quote from: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'. Is this an accurate statement? Even if there's like 20+ properties? That would be 40+ methods and that's not including doing anything with those properties but setting/getting. That sounds...I don't know...against the grain. Hi, can you help me please? I have some code written about 5 yrs ago that is for example like this echo"<div class=\"select\">"; echo"<br /><br />\n"; echo"</div>\n"; echo"</div>\n"; echo "<p><b>$address</b></p>\n"; Does it make any difference at all if I amended it to echo"<div class=\"select\"><br /><br /></div></div><p><b>$address</b></p>"; ie coding errors or increase load time? also when should I use \n"; or "; Please forgive my ignorance but I am a self taught php writer and have managed to teach myself but just need a little help. Cheers is it okay to name a column "date" in a table ??? just want to know whether this could mess up everything thanks in advance Hello, Im from the Netherlands and i have a little question about php. I have a form in php where administrators can make new users. They can fill in a name, password, age and can select a role. The role is a dropdown menu where they can select athlete, researcher and trainer. My problem is that when they select a role, i want that selected role transfert to a php variable. I want to use the variable in a if statment, when sporter selected a new dropdown appears with options like tennis, hockey.... I have seen a lot on google about this with jquery and stuff, but i cant find a good example. Can u help me? Thanks. I would like to keep all my classes in a folder placed within the htdocs folder where my php script runs. Let's say the classes folder is com\jimslounge\classes. From my script can I instantiate a class like this $myClass = new com\jimslounge\classes\Jimsclass(); Does it work something like that? I would hate to have to use include to access classes. Hello Everyone, i am fairly new in PHP coding, and facing some issue with include function in php. my problem is as follows: in a php script which is located at "/include/cart.php" i am including a html page "cart_add.html" which is at location "/cart_add.html" now whenever i am clicking on any links on this page "cart_add.html" browser looking for all other pages at location "/include/" instead of "/" so in the browser i see url as "http://localhost/include/index.html" instead of "http://localhost/index.html" so i get error like this : [an error occurred while processing this directive] The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please help me to resolve this issue. Thanks very much Hi: 1.I have some scripts in a dir of my root directory that i wanted to move to a subdir in it . 2.So i created a dir "Set1" and moved them there. 3.Now each of these files has an include at the top which contains db info and another which contains session info etc). 4.After redirecting to the new location I modified the include as under and got errors - as listed below. 5.It appears that when a script in the new location is to be processed, it cannot find the db info. 6.It searches for the db info in the subdir and not in the main dir. I tried fiddling with the ../ stuff but havent succeeded. Any pointers please ? Many thanks:Swati ========================= /www/NEW contains connectionlink.php loglink.php /www/NEW/Set1 contains hr.php The header of hr.php is as under : <? header("Cache-Control: public"); include ("../NEW/connectionlink.php"); //db info include ("../NEW/loglink.php"); //loginfo ?> Errors that come up are as under. Lines 5, 6 of hr.php have the include statements: Code: [Select] Warning: include(../NEW/connectionlink.php) [function.include]: failed to open stream: No such file or directory in /home/tern/public_html/NEW/Set1/hr.php on line 5 Warning: include(../NEW/connectionlink.php) [function.include]: failed to open stream: No such file or directory in /home/tern/public_html/NEW/Set1/hr.php on line 5 Warning: include() [function.include]: Failed opening '../NEW/connectionlink.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/tern/public_html/NEW/Set1/hr.php on line 5 Warning: include(../NEW/loglink.php) [function.include]: failed to open stream: No such file or directory in /home/tern/public_html/NEW/Set1/hr.php on line 6 Warning: include(../NEW/loglink.php) [function.include]: failed to open stream: No such file or directory in /home/tern/public_html/NEW/Set1/hr.php on line 6 Warning: include() [function.include]: Failed opening '../NEW/loglink.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/tern/public_html/NEW/Set1/hr.php on line 6 Sorry, Please login and use this page <?php if (!isset($_GET['register'])) { echo "register stuff here"; } elseif (!isset($_GET['login'])) { echo "login stuff here"; } elseif (!isset($_GET['forgotpassword'])) { echo "password recovery stuff here"; } else { echo "default page stuff here"; } ?> you get what i'm going for, but it kinda messes up this way with different contents on different places? help I have several fields in a database in which some could be blank. I'd like to echo out 'Not Assigned' if that's the case. Based on my limited experience with PHP this is the way I'm going about it: Code: [Select] if ($tech == "") { $tech = "Not Assigned"; } if ($crew == "") { $crew = "Not Assigned"; } if ($number == "") { $number = "Not Assigned"; } I'm pretty certain there is a way to simply place all of the variables into an array to determine if they are empty or not - Followed by changing the variable to 'Not Assigned' one time rather then repeating code over and over again. The way I'm doing it will work, however, something tells me there is a much more efficient way. I have inherited some pages from someone else who wrote them and as I am a newbie and the code is quite old I suspect it needs to be updated in order to work. I have two variables $p and $service that were passed from page 1 to page 2 via the URL. On page 2 I have updated the code for echo $_GET['$p'] etc and I am happy with that. Lower down on page 2 within an HTML form the author has Code: [Select] <input type="hidden" name="service" value="<? echo $service; ?>" /> <input type="hidden" name="p" value="<? echo $p; ?>" /> with the intention of putting these variables in the URL for page 3 I have to assume. Page 3 begins with this code Code: [Select] "completeyourorder.php?p=" . $p . "&service=" . $service; but the variable values are not showing up in the URL and I can not seem to use them on page 3. In other words they don't seem to be available to me. I'd appreciate help to change the page 3 code above or the page 2 code so that it works as intended. I found some posts and researched tutorials but all that I have tried has not worked. Thanks Glenn probably a very stupid question, but i pose it because it's counterintuitive for me to write it: i find in alot of wordpress code if statements like: if ( false === $var) i suppose it's the same as if ( $var === false) ? why would one right it in the first way? Hello!
I'm newbie in PHP(started 3 days ago), and i have two questions about the same topic, i hope you can answer me.
1- Is there methods in php for interaction with webpages as javascript, as they simulate a user interacting with these pages? For example, the method click () or the getElementsByClassName () for you to find something specific on the page. There are methods that do this in php?
2- To interact with a website using a sript (to click on links or save the url of pictures in an array) i start "JavaScript Scratchpad" Firefox with Shift + f4 and then it runs the script there, is just be on the page i want. If i want to do something similar in php, how can i run this script on my browser or is it done another way?
Thanks!
|