Can't set CGI Parameter (PHP) - php

I'm new to PHP and have a problem. I have a navbar on the side of a homepage that displays 4 links. I'm supposed to write a method setWhichPage in a child class that will access a CGI parameter named 'whichpage' from a parent class and use it to determine which of the 4 linked pages will display in the main section of the homepage, using another method called getMainFunction. The navbar was created with the methods getLeftNavBar and createNavbarArray, which exist in the child class (called TravelAgent), but get their variables from the parent class (called Company). The method setWhichPage is supposed to be in this child class also. I'm supposed to use the value $_GET or $_REQUEST array to set the value in the property $whichpage. The acceptable values for the $whichpage property should match the getLeftNavBar method. The methods getLeftNavBar and setWhichPage work together so that when the user clicks on a link in the navbar the URL they are supposed to create these links:
http://amazingadventures.000webhostapp.com/?whichpage=home
http://amazingadventures.000webhostapp.com/?whichpage=sales
http://amazingadventures.000webhostapp.com/?whichpage=support
http://amazingadventures.000webhostapp.com/?whichpage=contact
Where I'm having trouble is when I try to get setWhichPage to set the variable $whichpage to one of the four values in the links, I get this error message:
Notice: Undefined index: whichpage in /storage/ssd1/873/8888873/public_html/index.php
Can you tell me what I'm doing wrong? Thanks in advance for any help. Here's the relevant code:
<?php
class Company { //start of parent class Company
protected $company_name;
protected $company_address;
protected $company_url;
protected $company_email;
protected $whichpage;
public function __construct(){
$this->company_name = "Amazing Adventures Travel";
$this->company_address = "13999 Turtle Way, Los Angeles, California, 90001";
$this->company_url = "http://amazingadventures.000webhostapp.com/";
$this->company_email = "email.edu";
$this->whichpage = "home";
}
//other methods...
} //end of parent class Company
class TravelAgent extends Company { //start of child class TravelAgent
var $navbar_array;
function create_navbar_array() {
$mainurl = $this->company_url;
$this->navbar_array = array("Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact");
}
function getLeftNavBar() {
echo "<table border=1, td width=100, height = 40><tr>";
foreach($this->navbar_array as $x => $x_value) {
echo '<tr><td>'. $x ."</td></tr>";
echo "<br>";
}
echo "</tr></table>";
}
function setWhichPage(){
$whichpage = $_GET['whichpage'];
}
function getMainSection() {
echo "The ".$whichpage." page";
}
}
//end of child class TravelAgent
$travelobject = new TravelAgent(); //object creation
$travelobject->getHeader(); //function calls
$travelobject->create_navbar_array();
$travelobject->getLeftNavBar();
$travelobject->setWhichPage();
$travelobject->getFooter();
?>
<table style='width:100%' border='1'> //HTML
<tr>
<td style='width:15%'>Left Navigation Bar</td>
<td> getMainSection()</td> //function call
</tr>
</table>

You have to reference it with $this->whichpage and check for $_GET['whichpage'] if is set. Something along the lines like this:
function setWhichPage(){
if(isset($_GET['whichpage'])) {
$this->whichpage = $_GET['whichpage'];
}
}
function getMainSection() {
echo "The ".$this->whichpage." page";
}

Notice: Undefined index: whichpage in /storage/ssd1/873/8888873/public_html/index.php
This error means $_GET array does not have the key whichpage.
In your case if you open http://amazingadventures.000webhostapp.com/ the $_GET array will be empty.
The best practice is to check whether the key exists before accessing.
Example of setWhichPage:
$this->whichpage = isset($_GET['whichpage']) ? $_GET['whichpage'] : $this->whichpage; // "home" by default

Related

PHP - Inheritance using Child Class. Changing multiple values in a Class with the Child Class

I am trying to change the two values, 'first' and 'last' name, using a child class. My textbook only shows how it works if there is only one variable in the main class. I have tried to expand on it and have had to just guess at how it is done when using multiple variables/values. I have tried variations on this and check it with a browser through a server.
<?php
class contactInfo {
public$first = "Robert";
public$last = "Heston";
public$address = "3289 Westmore Blvd.";
public$city = "Greensboro";
public$state = "NC";
public$zip = "27128";
/* The example in the book had the one variable which would be the same as my 'first' and it said the function had to have the same name as the class and it had equivilant of "$ft" in parenthesis and used "$this->first = $ft;" with it. I tried expanding on that and this stuff is just no good. */
function contactInfo($ft, $lt, $as, $cy, $se, $zp) {
$this->first = $ft;
$this->last = $lt;
$this->address = $as;
$this->city = $cy;
$this->state = $se;
$this->zip = $zp;
}
function showMixedInfo() {
echo "My name is ".$this->first." ".$this -> last."<br/>";
echo "I live at ".$this ->address."<br/>";
echo "and my city and state are".$this->city." ".$this->state."<br/>";
echo "and my zip code is".$this->zip."<br/>";
}
}
/* I created the child class and the variables below the only way I know how. In the book, '$newInfo = new childClass()' had the actual name change in the parenthesis, but I need to change 2 values. */
class childClass extends contactInfo {
//Maybe there needs to be some code here?
}
$newInfo = new childClass();
$this->first("Graham");
$this->last("Rouse");
$newinfo -> showMixedInfo();
?>
The instructor's "hint" used the child class like this instead and I had no more idea about how to create everything else to go with it than with what I have above:
class childClass extends contactInfo {
function showMixedInfo($ft, $lt) {
$this->first = $ft;
$this->last = $lt;
echo "My name is ".$this->first." ".$this -> last."<br/>";
echo "I live at ".$this ->address."<br/>";
echo "and my city and state are".$this->city." ".$this->state."<br/>";
echo "and my zip code is".$this->zip."<br/>";
}
}
I am at a loss with this one. This instructor gives assignments beyond the scope of the textbook then insists it's in there when no amount of rereading/studying reveals it. I have searched for solutions prior to this.

Initating an Array inside a Class

I have been having a hella of a time trying to get an array to initialize inside a class. What I am trying to attempt is to create a side menu using an array and a child class. I have tried to solve this problem for several days and now I'm asking for help. I have a feeling I am missing something basic as I am just learning PHP.
Below is my code and the commented out lines are attempts at a solution that have not worked.
<?php
/************* global variables ******************************/
//global $house_array, $car_array, $vacation_array, $company_address;
$company_name = "Heaven HVAC";
$street = '12345 Blue Canyon Rd.';
$company_citystatezip = "Heaven, CA 91777";
/************* end global variables **************************/
echo '<H1 align="center">Calling Array inside Child Class</H1>';
class Company{
//// insert object variables (properties) HERE
var $company_url = "http://localhost";
var $company_email = "example#example.com ";
//// insert methods here
function getHeader($company_name, $color) {
$topheader = "<TABLE align='center'; style='background-color:$color; width:50%'><TR><TD>";
$topheader .= "<H1 style='text-align:center'>$company_name</H1>";
$topheader .= "</TD></TR></TABLE>";
return $topheader;
}
function getFooter($color) {
$this->address;
$bottomfooter = "<TABLE align='center'; style='background-color:$color;width:50%'><TR><TD>";
$bottomfooter .= "<center><b><u>$this->address</center></b></u>";
$bottomfooter .= "</TD></TR></TABLE>";
return $bottomfooter;
}
} // end class Company
//// Working On child class - If commented out, then the code has been tried and failed
class AirCondition extends Company {
var $navbar_array;
//// create array
function create_navbar_array ( ) {
$mainurl = $this->company_url;
$this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
}
//create_navbar_array();
// return $navbar_array;
function getLeftNavBar($array){
// create_navbar_array();
## Create a table to display arrays
print "<TABLE BORDER='1'>";
echo '<tr><td>Navigation Menu</td></tr>';
foreach($array as $Page){
echo "<tr><td>{$Page}</td></tr>";
return $array;
}
echo "</TABLE>";
}
}
$HVACcompany = new AirCondition();
$HVACcompany->address = "777 Estate Rd <br /> $company_citystatezip";
echo $HVACcompany->getHeader($company_name, orange);
echo "<br/>";
echo $HVACcompany->getFooter(green);
//echo $HVACcompany->getLeftNavBar();
//echo $HVACcompnay->create_navbar_array();
//$HVACcompnay->create_navbar_array();
$HVACcompany->getLeftNavBar($navbar_array);
?>
I am able to create the web page layout, but the array isn't initializing. What am I doing wrong/missing?
The following line:
$HVACcompany->getLeftNavBar($navbar_array);
should be:
$HVACcompany->getLeftNavBar($HVACcompany->navbar_array);
Because $navbar_array was never declared in the global namespace, however it was declared as a public variable within the class AirCondition which was initialized with the variable $HVACcompany.
Additionally you need to call the function $HVACcompany->create_navbar_array();
before calling $HVACcompany->getLeftNavBar($HVACcompany->navbar_array); in order to create the array $navbar_array.
Also remove the return $array; statement inside the foreach loop.

Codeigniter passing data controller to view

Here is my controller:
class CommonController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('common_model'); //load your model my model is "common model"
}
public function add_work(){
$names = $_POST['name'];
$works = $_POST['work'];
$allValues = array(); // array to contains inserted rows
foreach($names as $key => $name){
$name= "your specified name";
$insertdata = array();
$insertdata['work'] = $works[$key];
$insertdata['name'] = $name;
$this->common_model->insert($insertdata);
array_push($allValues,$insertdata);
//$insert = mysql_query("INSERT INTO work(name,work) values ( '$name','$work')");
}
foreach($allValues as $insertRow){
echo $insertRow['work'];
echo $insertRow['name'];//this shows data well. but how to pass data in view.php
}
//view code will add here to show data in browser
}
Basically I want to pass all data to view.php for printing or exporting purpose. How can I do so.
To load a view you should do like this.
$this->load->view("filename");
If you want to pass data to view, you should do like this.
$this->load->view("filename",$data);
$data should have all parameters which you want to print in view.
The syntax goes like this.
$this->load->view("filename","data to view","Returning views as data(true / false");
If third parameter is true, view will come as data. It will not go to browser as output.
Edit:
Change
$this->load->view('print_view',$insertdata);
to
$data['insertdata'] = $insertdata;
$this->load->view('print_view',$data);
For more info, check this link
How CI Classes Pass Information and Control to Each Other
Calling Views
We will see.how the controller calls a view and passes data to it:
First it creates an array of data ($data) to pass to the view; then it loads and calls the view in the same expression:
$this->load->view('testview', $data);
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a
model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
$foo = $this->mymodel->myfunction();
and the function is set to return a value, then that value will be passed to the variable $foo inside the controller.
//sample
public function display()
{
$data['text_to_display'] = $this->text_to_display;
$data['text_color'] = $this->text_color;
$this->load->view('display_view',$data);
}
Adding Dynamic Data to the View
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view
loading method. Here is an example using an array:
$data = array(
’title’ => ’some’,
’heading’ => ’another some’,
’message’ => ’and another some’
);
$this->load->view(’view’, $data);
And here’s an example using an object:
$data = new Someclass();
$this->load->view(’view’, $data);
Sending Multiple Dimensional array
if we pull data from your database it will typically be
in the form of a multi-dimensional array.
<?php
class foo extends CI_Controller {
public function index()
{
$data[’Books’] = array(’POEAA’, ’TDD’, ’Clean C’);
$data[’title’] = "Title";
$data[’heading’] = "Heading";
$this->load->view(’view’, $data);
}
}
in view
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
<h3>My Books List</h3>
<ul>
<?php foreach ($Books as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>
More Learning
NOTE:
There is a third optional parameter lets you change the behavior of the method so that it returns data as a string rather
than sending it to your browser.The default behavior is false, which sends it to your browser. Remember to
assign it to a variable if you want the data returned:
$string = $this->load->view(’view’, ’’, TRUE);
Above will not solve your problem directly but definetly help in understanding concepts.

php pdo in class + best method to display return array

I have the following class:
<?php
class photos_profile {
// Display UnApproved Profile Photos
public $unapprovedProfilePhotosArray = array();
public function displayUnapprovedProfilePhotos() {
$users = new database('users');
$sql='SELECT userid,profile_domainname,photo_name FROM login WHERE photo_verified=0 AND photo_name IS NOT NULL LIMIT 100;';
$pds=$users->pdo->prepare($sql); $pds->execute(array()); $rows=$pds->fetchAll();
$unapprovedProfilePhotosArray = $rows;
echo 'inside the class now....';
foreach($rows as $row) {
echo $row['userid'];
}
}
}
I can display the data successfully from the foreach loop.
This is a class that is called as follows and want to be able to use the array in the display/view code. This why I added the "$unapprovedProfilePhotosArray = $rows;" but it doesn't work.
$photos_profile = new photos_profile;
$photos_profile->displayUnapprovedProfilePhotos();
<?php
foreach($photos_profile->unapprovedProfilePhotosArray as $row) {
//print_r($photos_profile->unapprovedProfilePhotosArray);
echo $row['userid'];
}
?>
What is the best way for me to take the PHP PDO return array and use it in a view (return from class object). I could loop through all the values and populate a new array but this seems excessive.
Let me know if I should explain this better.
thx
I think you're missing the $this-> part. So basically you're creating a local variable inside the method named unapprovedProfilePhotosArray which disappears when the method finishes. If you want that array to stay in the property, then you should use $this->, which is the proper way to access that property.
...
$pds=$users->pdo->prepare($sql); $pds->execute(array()); $rows=$pds->fetchAll();
$this->unapprovedProfilePhotosArray = $rows;
...

PHP Concrete 5 Pass Variables to Add.php

I'm creating a new block and I want to pass a defined variable to the block instance on add.
In my controller, I have the following:
// declare the var
public $hasMap = 0;
public function add() {
$this->set('hasMap', $this->generateMapNumber());
}
The generateMapNumber() function looks like this:
public function generateMapNumber() {
return intval(mt_rand(1,time()));
}
In my add.php form I have a hidden field:
<?php $myObj = $controller; ?>
<input type="hidden" name="hasMap" value="<?php echo $myObj->hasMap?>" />
When I create a new block, hasMap is always 0 and the hidden input value is always 0 too. Any suggestions? Thank you!
--- EDIT ---
From the concrete5 documentation:
// This...
$controller->set($key, $value)
// ... takes a string $key and a mixed $value, and makes a variable of that name
// available from within a block's view, add or edit template. This is
// typically used within the add(), edit() or view() function
Calling $this->set('name', $value) in a block controller sets a variable of that name with the given value in the appropriate add/edit/view file -- you don't need to get it from within the controller object. So just call <?php echo $hasMap; ?> in your add.php file, instead of $myObj->hasMap.
It will not be the same value, because the function will give diferrent values every timy it is called.
So here's the solution. In the controller...
public $hasMap = 0;
// no need for this:
// public function add() { }
public function generateMapNumber() {
if (intval($this->hasMap)>0) {
return $this->hasMap;
} else {
return intval(mt_rand(1,time()));
}
}
And then in the add.php file...
<?php $myObj = $controller; ?>
<input type="hidden" name="hasMap" value="<?php echo $myObj->generateMapNumber()?>" />
It works perfectly. On add, a new number is generated and on edit, the existing number is drawn from the hasMap field in the db.
Thanks for all the input. Hope that helps someone else!

Categories