Warning: Undefined array key "Item_ID" - php

I'm having a problem with PHP code. I got some code from an e-book, but when I tried to run the system, it's giving some errors. I fixed some of the errors by giving proper quotation marks, but now I'm stuck with some other errors.
I'd be more than happy if someone helps me with the following error:
Warning: Undefined array key "Item_ID" in C:\xampp\htdocs\eCommerce\order.php on line 13
This is my code for order.php:
<?php
ob_start();
session_start();
if (isset ($_SESSION['Username'])){
$pageTitle ='Order';
include 'init.php';
?>
<div class="container">
<div class="row">
<?php
$Allitem = getAllFrom('items','Item_ID');
if ($_GET['Item_ID']){
foreach($Allitem as $item){
echo '<div class="col-sm-6 col-md-4">';
echo '<div class="thumbnail item-box">';
echo '<span class="price-tag">$ ' . $item['Price'] . '</span>';
echo ' <img class="img-responsive" src="../eCommerce/admin/layout/images/'. $item['Image'].' " alt="No Image Uploaded" />';
echo '<div class="caption">';
echo '<h3 class="items-name">'.$item['Name'] .'</h3>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
}
?>
</div>
<?php
include $tpl .'footer.php';
ob_end_flush();
?>

You are calling the index which is not in you $_GET parameters. You have to verify that the parameter is present or not. For this you have to change your if condition from
if($_GET['Item_ID']){
....
}
to
if(isset($_GET['Item_ID'])){
....
}
this will verify that the required parameter is present or not

Related

php mysql while loop not displaying first row

my code is not displaying the first result in the while loop.
my query is working right on phpmyadmin but somethings going wrong on index page.
I have the following code:
<?php
$test=mysql_query("SELECT * FROM newsites");
$deneme=mysql_fetch_row($test);
?>
<div class='container'>
<div class='row'>
<?php
while ($deneme=mysql_fetch_assoc($test)) {
extract($deneme);
echo '<div class="col-md-3 col-sm-6 col-xs-12 back-colour">';
echo '<td><img class="img-responsive" src="images/'.$deneme['site_pic'].'" width="120" height="20"/></td>';
echo '<p class="box-design">'.$deneme['site_name'].'</p>';
echo '<p class="box-design">'.$deneme['site_link'].'</p>';
echo '<p class="box-design">'.$deneme['site_ref'].'</p>';
echo '<p class="box-design">'.$deneme['site_type'].'</p>';
echo '</div>';
}
?>
</div>
</div>
It looks like the first row is already taken from the resultset outside the while loop.
On line 3 you have $deneme=mysql_fetch_row($test);
I'd say remove it and you're good to go.

What is wrong with my if statement?

I had the below code which worked fine until I added an if statement to restrict the loop to only run on certain items in the array. I am now only getting a blank page which suggests there is an error somewhere in my code since adding the if statement, but I can't figure out where.
I'd really appreciate any help on solving this, as well as suggestions on how I could have solved myself (I'm still new to PHP and not sure how to effectively debug this type of issue).
Nb. There is an opening <?php tag not shown in the below snippet.
foreach ($portfolioProjects as $portfolio) {
if ($portfolio['featureContent'] == "Yes") {
?>
<div class="row featurette">
<?php
//for each odd number, change the layout so it looks nice :)
if ($loopCount % 2 == 0) {
echo '<div class="col-md-7">';
} else {
echo '<div class="col-md-7 col-md-push-5">';
}
?>
<h2 class="featurette-heading"><?php echo $portfolio[title]; ?> <span class="text-muted"><?php echo $portfolio[languages]; ?></span>
<?php
//Check array for newTag which will be added to show a tag to the user
if ($portfolio[newTag] == "Yes") {
echo '<span class="label label-success test pull-right"> New!</span>';
}
?></h2>
<p class="lead"><?php echo $portfolio[blurb]; ?></p>
</div><!--end of column1-->
<?php
if ($loopCount % 2 == 0) {
echo '<div class="col-md-5">';
} else {
echo '<div class="col-md-5 col-md-pull-7">';
}
?>
<img class="featurette-image img-responsive center-block" data-src="holder.js/200x200/auto" alt="200x200" src="assetts/200x200.gif" data-holder-rendered="true">
</div>
</div>
<?php
//if statement to stop divider being added if last item in array
$loopCount = $loopCount + 1;
if ($loopCount != $itemsCount) {
echo '<hr class="featurette-divider">';
}
}
}
?>
The easiest way to find out what the problem is, to check your webserver's log file, or turn on error_reporting and display_errors in your php.ini file.
Are newTag and blurb constants or keys in the $portfolio array? If they are keys, you should use apostrophes.
if ($portfolio['newTag'] == "Yes") {
and
<p class="lead"><?php echo $portfolio['blurb']; ?></p>
You forgot to wrap array keys in quotes
`$portfolio['title']
$portfolio['languages']
$portfolio['newTag']
$portfolio['blurb']`

HTML inside PHP not working

I have been trying to put HTML inside PHP for a while, but I could never get it to work. Instead I have had to use this
<?php //code in here
while($record = mysql_fetch_array($myData)){
?>
//HTML code
<?php
}
?>
and it has worked for a while, but now I'm implementing a search function to the page, and I need all HTML to be dynamically generated, or the page will be displayed twice. Therefore, i'm in need of some help. This is the old-new code I have at the moment.
<?php
$sql = "SELECT * FROM `LISTINGS` ORDER BY YA DESC $limit";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
//this is what i have manage to get done
echo '<div class="cards-row">';
echo '<div class="card-row">';
echo '<div class="card-row-inner">';
echo '<div class="card-row-image" data-background-image="http://domains/$record["IMAGENAME"].jpg">';
echo '</div>';
echo '<div class="card-row-body">';
echo '<h2 class="card-row-title">';
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
echo '</a>';
echo '</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
?>
Static/dynamic code
<div class="card-row">
<div class="card-row-inner">
<div class="card-row-image" data-background-image="http://www.domain/<?php echo $record['IMAGENAME'];?>.jpg" width="150" height="180" ">
</div><!-- /.card-row-image -->
<div class="card-row-body">
<h2 class="card-row-title">
<a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>"> <?php custom_echo ($record['TITLE'], 80); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?>
</a>
</h2>
<div class="card-row-content"><?php custom_echo ($record['DESCRIPTION'], 250); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?></div><!-- /.card-row-content -->
</div><!-- /.card-row-body -->
<div class="card-row-properties">
<dl>
<dd></dd><dt>Visit Website</dt>
<dd></dd><dt>More Info</dt>
<dd>Added</dd><dt><?php echo $record['YA'];?>/<?php echo $record['MU'];?>/<?php echo $record['DU'];?></dt>
<dd>Viewed</dd><dt>Visited</dt>
</dl>
</div><!-- /.card-row-properties -->
</div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
</div><!-- /.cards-row -->
<br/>
<?php
}
?>
My goal is to put all the code above inside
while($record = mysql_fetch_array($myData)){
Although this is not the best way when it comes to readability and maintainability, the way I usually process HTML with PHP is by echoing it.
echo "<div style=\"color:red\">test</div>";
If I need to add PHP content then
$myVar = "test";
echo "<div style=\"color:red\">" . $myVar . "</div>";
And if I already have some HTML code I'd like to escape, I can easily do so using Notepad++'s replace all function by replacing " with \"
You have an error in line 15
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
Change it to:
echo '<a href="http://domain/listing-detail.php?ID='.$record['ID'].'">';
Also, are you saving it in ".php" extension?.
After the last echo, you forget a '}'.
Finally, remove your mysql_ function and use mysqli_ or PDO, if you're on PHP7.

Adding Anchor to every module in Joomla 3.1

I am trying to modify the module template code to add an anchor to the title of each module. I have extremely limited PHP knowledge, so any guidance would be very helpful.
I've found posts for doing this with articles, but the code appears to be very different for modules.
This is the code that is in my module template.
function modChrome_basic($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<?php echo $module->content; ?>
<?php endif;
}
function modChrome_standard($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="rt-block <?php if ($params->get('moduleclass_sfx')!='') : ?><?php echo $params->get('moduleclass_sfx'); ?><?php endif; ?>">
<div class="module-surround">
<?php if ($module->showtitle != 0) : ?>
<div class="module-title">
<?php
echo '<h2 class="title">';
if (preg_match("/icon[-]{1,}/i", $params->get('moduleclass_sfx'))) :
echo '<span class="title-icon ' .getIconClass($params->get('moduleclass_sfx')). '"></span>';
endif;
echo $module->title;
echo '</h2>';
?>
</div>
<?php endif; ?>
<div class="module-content">
<?php echo $module->content; ?>
</div>
</div>
</div>
<?php endif;
}
This is the code I tried
if( strlen($this->item->params->get('title')) > 0 ) {
echo '<a name="'.$this->item->params->get('title').'"></a>';
}
I also tried
if( strlen($module->item->params->get('title')) > 0 ) {
echo '<a name="'.$module->item->params->get('title').'"></a>';
}
Joomla has an easy way to customize the module appearance.
You have to use a Custom module chrome.
You have to create a modules.php file under /templates/TEMPLATE_NAME/html/modules.php.
Inside this file you have to put:
<?php
function modChrome_custom( $module, &$params, &$attribs ) {
echo '<div id="' .$params->get( 'moduleclass_sfx' ) .'" >';
if ($module->showtitle)
{
echo '<h' .$headerLevel .'>' .$module->title .'</h' .$headerLevel .'>';
}
echo '<div class="module_content">';
echo $module->content;
echo '</div>';
echo '</div>';
}
?>
In the code above the module id getting moduleclass_sfx value. You could change it and set another module variable.
In order to use this appearence you have to set the appropriate style to the template module calls like: <jdoc:include type="modules" name="user1" style="custom" />
If you want to use another style name you have to change the function from modChrome_custom to modChrome_NEWSTYLE.
Good Luck!

Am I missing an escape for one of these quotes?

I'm not entirely sure why this isn't working. But this piece of code does not work.
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'"><?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?></div>';
}
?>
I was guessing that maybe I'm putting the wrong quotes in the wrong place.
All the parts seperately work, as in :
I can display the 'gid' value with echo $gallery['gid']
I can make the div tags appear with the appropriate rel
I can, by itself, make <?php echo do_shortcode("[nggallery id=3]"); ?> work.
I just can't make the entire thing appear together.
You're mixing interpolated php and html, placing "<?php echo" inside what's already php.
<div class="tgallery" rel="<?php echo $gallery['gid'];?>">
<?php echo do_shortcode('[nggallery id="'.$gallery['gid'].'"]'); ?>
</div>
Why you put <?php ?> inside your echo ?
<?php
foreach ( $gallery_ids as $gallery )
{
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode('[nggallery id='.$gallery['gid'].']').'</div>';
}
?>
The issue
Pick either string concatenation or opening/closing PHP for HTML. You cannot combine both as you have done above.
echo '<div class="tgallery" rel="'.$gallery['gid'].'">
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
</div>';
The second line of code above does not belong inside a string as code in between the <?php ... ?> will not be parsed by PHP when it is contained in a string.
Solutions
Concatenation
I have fixed your code to use concatenation below:
foreach ( $gallery_ids as $gallery ) {
$shortcode = do_shortcode("[nggallery id={$gallery['gid']}]");
echo '<div class="tgallery" rel="' . $gallery['gid'] . '">' . $shortcode . '</div>';
}
Opening and closing PHP
This is how you would do it using PHP "templating":
<?php foreach($gallery_ids as $gallery ): ?>
<div class="tgallery" rel="<?php echo $gallery['gid']; ?>">
<?php echo do_shortcode("[nggallery id={$gallery['gid']}]"); ?>
</div>
<?php endforeach; ?>
You are already "in" php, so your opening tag is causing the problem:
<?php echo do_shortcode("[nggallery id='.$gallery['gid'].']"); ?>
It should be something like:
echo '<div class="tgallery" rel="'.$gallery['gid'].'">' . do_shortcode('[nggallery id='.$gallery['gid'].']') . '</div>';
<?php
foreach ( $gallery_ids as $gallery ) {
echo "<div class=\"tgallery\" rel=\"{$gallery["gid"]}\">". do_shortcode("[nggallery id=\"{$gallery["gid"]}\"]") ."</div>";
}
?>
try this....
<?php
foreach ( $gallery_ids as $gallery ) {
echo '<div class="tgallery" rel="'.$gallery['gid'].'">'.do_shortcode("[nggallery id=".$gallery['gid']."]").'</div>';
}
?>
you have inside a php statment
Try Notepad then it will colour code your php code, so you can clearly see what quotes etc you have wrong

Categories