Why do I get undefined index - php

I am trying to pass a variable between two webpages via a url and using php. I am getting the undefined index error.
I have tried multiple ways of doing this and spent the last several hours googling the problem and think the following code should work -- probably just a conceptual error. Am not using ISSET at this point because this is a url pass and it should be set.
The following code is on the passing page:
<DIV>
<ul>
<li> Christmas Eve, 2014 ; </li>
</ul>
</DIV>
The following code is on bulletindisplay.php
<div>
<?php
$link = $_GET['link'];
echo $link;
?>
</div>
Does anyone see my error? Thank you so much for any help.

Remove all spaces inside href attribute:
<DIV>
<ul>
<li> Christmas Eve, 2014 ; </li>
</ul>
</DIV>

Just remove spaces inside your link.
bulletindisplay.php?link=Bulletins/December_24_2014.pdf

Remove all spaces and use below code:
<li> Christmas Eve, 2014 ; </li>
and in php :
if(isset($_GET['link'])) {
$link = $_GET['link'];
echo $link;
}

link = Bulletins/December_24_2014.pdf
//there is a space after link
If you print out $_GET you will get output like this
Array ( [link_] => Bulletins/December_24_2014.pdf )
So
$_GET['link'] !=$_GET['link_']
//$_GET['link'] is not set that's why you got the error

You need to remove the space after link in your URL. And you should be using isset because if link is not present in the query string you will see a PHP error.
1. if(isset($_GET['link'])) {
2. $link = $_GET['link'];
3. echo $link;
4. }

Related

PHP advice with defining a GET Method

I have a navigation like this:
<div class="navigation" id="<?php echo $page ?>">
<ul>
<li>`<a class="home" href="index">Home</a>`</li>
<li>`<a class="training-gallery" href="training-gallery">Training Gallery</a>`</li>
<li>`<a class="products-and-merchandise" href="products-and-merchandise">Products & Merchandise</a><`/li>
<li>`<a class="facebook-page" href="facebook-page">Personal Training on Facebook</a>`</li>
<li>`<a class="personal-training-forum" href="client-feedback">Client Feedback</a>`</li>
<li>`<a class="dannys-blog" href="dannys-blog">Danny's Blog</a>`</li>
</ul>
</div>
in my page, I have put the following line of code at the top of the page of a page called training gallery:
<?php $page ='training-gallery' ?>
On my website when I inspect the element using firebug, I get a message like:
Notice: Undefined variable: page in /Applications/XAMPP/xamppfiles/htdocs/Websites/twd/dtpt/includes/navigation.php on line 1 on the link of the training gallery page.
I am assuming I need a GET method instead of this simple bit of code to say that if you are on the training-gallery page, put the name of the page training-gallery on the navigation id line in the code. by defining where the page is. Hopes this makes sense.
Yes, you do need a GET parameter :-)
In the link, you can specify: <a href="?page=home
and in php
<?php
$page = (isset($_GET['page'])) ? $_GET['page'] : '';
this will check, whether the $_GET['page'] exists and if not, it will set the $page to empty string...

PHP show text up to /*show more text*/

I would like to only show the text that is before /*SHOW MORE TEXT*/ However I am unsure how to do such a thing because my text is like this:
<ul>
<li>show this</li>
<li>show this</li>
/*SHOW MORE TEXT*/
<li>don't show this</li>
</ul>
I have tried
$texts = explode("/*SHOW MORE TEXT*/", $text);
But the issue with this is then it won't fix the </ul>
How can I fix this small issue?
Just try with Jquery toggle functions it's more easier and faster. Here you find details: http://api.jquery.com/toggle/
Using php you can do this like
<ul>
<li></li>
<?php
if(condition is true){
echo "<li>text will be displayed if condition is true otherwise it will not displayed</li>";
}
?>
</ul>
using javascript you can use
<li style="display:none;">this will not visible</li>
you can use display:none; property for that and later when you need it you can display it using java script.
<li style="display:none;">don't show this</li>
One possible solution is as follows:
Don't include ul tags in the text string, and then use explode and take the first element of array using array[0] and close </ul> tag.

Why is this XML attribute showing up twice

In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code

Calling an array of config items in CodeIgniter

I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.
I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:
$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";
$config['site'] = $site;
I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.
For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:
<li>
Home
</li>
I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?
Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks['MenuItems'] as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
try this
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>

Adding a Class to PHP Menus

I have a small php website that has a menu that is included via include_once. I need an HTML class added to the current menu item for each page.
So if a user clicks on a link and goes to that page the new page will load with the new menu item with class="current". How can I go about doing this?
Let's say you write your menu like this:
<ul>
<li> Link1 </li>
<li> Link2 </li>
<li> Link3 </li>
</ul>
Here is a good old php4 style solution :-p
<?php
//you get which link was clicked
if(isset($_GET['menu']) $menu = $_GET['menu'];
else $menu = 1;
$1 = $2 = $3 = ""; //you create 3 empty strings
switch($menu){ //you assign the correct string with your css class name
case "1": $1 = 'class="current"';break;
case "2": $2 = 'class="current"';break;
case "3": $3 = 'class="current"';break;
}
?>
//echo the variables into the menu anchors ( <?=$1?> is equivalent to <? echo $1; ?> )
<ul>
<li><a href="link1.com?menu=1" <?=$1?> >Link1</a></li>
<li><a href="link2.com?menu=2" <?=$2?> >Link2</a></li>
<li><a href="link3.com?menu=3" <?=$3?> >Link3</a></li>
</ul>
You can also place the <?=$1?> into the <li> or anywhere else wanted...
If you're using javascript in your project, you could also give an id to your anchors (like Link1), get the menu value like above with $_GET, and then use javascript to add the class to the desired link.
I gave you this answer because from what you're writing I guess you're not using any PHP framework but coding you app the old way... there's plenty of nice PHP framework around here that will have pre-made solution for this... might be hard at the beginning but it's worth it...
Good luck!

Categories