Unable to read GET variable when used in $page_content - php

I have a file called productFocus.php where I use the GET variable ID: $id = $_GET["id"];
However this page is used in the product.php file in the following manner:
product.php
<?php
$page_content = 'productFocus.php'; // $id = $_GET["id"];
include('master.php');
?>
productFocus.php
<?php
include "db/db.php";
$id = $_GET["id"];
$product = get_product_by_id($id);
?>
<div class="product-focus">
<h3><?php echo $product->name ?></h3>
<img src="/images/products/<?php echo $product->image ?>">
<div id="description">
<h4>Productinformatie</h4>
<p><?php echo $product->description ?></p>
<h4>Partners</h4>
<table>
<?php
foreach($product->partners_obj as $partner) {
?>
<tr>
<td>
<a href=<?php echo $partner->$product_url ?> target="_blank">
<img id="partner" src="/images/partners/<?php echo $partner->image ?>">
</a>
</td>
<td>
<?php $partner->$product_price ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
master.php
//HTML code
...
<?php include($page_content);?>
...
//HTML code
When I browse to productFocus.php?id=324324 I can read the GET variable, but when I browse to product.php?id=324324 I do not have access to the GET variable ID.
Is there an elegant way to solve this issue?

You need to check the GET variable before you call the product details in the prodcts.php file before regular page load.
<?php
if (isset($_GET['id'])) {
// GET PRODUCT DATA
}else{
// LOAD PRODUCTS PAGE
}
?>
As for getting the product details, I would suggest writing the separate call to the database, but if you just need to load page content, you are not calling it properly:
$id = $_GET['id'];
$fileurl = 'products.php?id='.$id;
$pagecontent = file_get_contents('$fileurl');

Related

Variable not being formatted with CSS - Magento

When I echo my own variable to a div container it comes out as plaint text (unformatted). I don't understand why echoing a Magento variable comes out formatted but mine doesn't? Here's my code, in particular the <?php if(!isset($specialPrice)): { ?> section which I created. Here is the code:
<div class="product">
<a href="<?php echo $_item->getProductUrl() ?>"
title="<?php echo $this->escapeHtml($_item->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail') ?>"
alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a>
<div class="product-details">
<p class="product-name">
<?php echo $this->escapeHtml($_item->getName()) ?>
</p>
<?php $specialPrice = $productToCheck->getData('special_price');
$orignalPrice = $productToCheck->getData('price');
?>
<?php if(!isset($specialPrice)): { ?>
<?php echo $product['price'] ?>
<?php } else: { ?>
<?php echo $specialPrice ?>
<?php } endif ?>
</div>
</div>
Echoing $product['price'] shows up with its CSS like this:
but if it enters the ELSE statement to display my variable it shows like this:
Does anyone know what could be going wrong?
$product['price'] returns you a unformatting price value.
Maybe you can call a block function, for example $block->getPrice() and in getPrice() you need to format the price by your custom preferences.
If you want to add styles to your price value then you need to use a magento tag class

PHP foreach - Creating multple searchs in the same page

Firstly, I am a total php newby.
Secondly, below is the php header plus corresponding recursive commands for 2 photo lightboxes that are in the same html page.
Without the lines
getRecords() as $Title_row){ ?>
The page shows shows the first picture that is held in the external db table in the lightbox correctly.
Yet when I add the line below to add all the other entries in each table
getRecords() as $Title_row){ ?>
the page goes white and the browser html return is empty.
As I do not really understand what I am doing, I am stuck. Any suggestions would be very welcome.
<?php
$Title_find = $Lowdenphotoweb->newFindCommand('MASTER_photo');
$Title_findCriterions = array('Order_web'=>'*',);
foreach($Title_findCriterions as $key=>$value) {
$Title_find->AddFindCriterion($key,$value);
}
fmsSetPage($Title_find,'Title',50);
$Title_find->addSortRule('Order_web',1,FILEMAKER_SORT_ASCEND);
$Title_result = $Title_find->execute();
if(FileMaker::isError($Title_result)) fmsTrapError($Title_result,"error.php");
fmsSetLastPage($Title_result,'Title',50);
$Title_row = current($Title_result->getRecords());
?>
<?php
$Title1_find = $Lowdenphotoweb->newFindCommand('MASTER_photo');
$Title1_findCriterions = array('Order_web'=>'*',);
foreach($Title1_findCriterions as $key=>$value) {
$Title1_find->AddFindCriterion($key,$value);
}
fmsSetPage($Title1_find,'Title',50);
$Title1_find->addSortRule('Order_web',1,FILEMAKER_SORT_ASCEND);
$Title1_result = $Title1_find->execute();
if(FileMaker::isError($Title1_result)) fmsTrapError($Title1_result,"error.php");
fmsSetLastPage($Title1_result,'Title',50);
$Title1_row = current($Title1_result->getRecords());
?>
<!DOCTYPE html>
<!-- Lightbox Gallery -->
<?php foreach($Title_result->getRecords() as $Title_row){ ?>
<div class="hide">
<a data-group="gallery-1" data-caption="<?php echo $Title_row->getField('Title'); ?>" href="<?php echo $Title_row->getField('Pic_location'); ?>"></a>
</div>
<?php foreach($Title2_result->getRecords() as $Title2_row){ ?>
<div class="hide">
<a data-group="gallery-2" data-caption="<?php echo $Title2_row->getField('Title'); ?>" href="<?php echo $Title2_row->getField('Pic_location'); ?>"></a>
</div>
<!-- Lightbox Gallery End-->
You just missed out the closing brackets :
<?php foreach($Title_result->getRecords() as $Title_row){ ?>
<div class="hide">
<a data-group="gallery-1" data-caption="<?php echo $Title_row->getField('Title'); ?>" href="<?php echo $Title_row->getField('Pic_location'); ?>"></a>
</div>
<?php }?>
<?php foreach($Title2_result->getRecords() as $Title2_row){ ?>
<div class="hide">
<a data-group="gallery-2" data-caption="<?php echo $Title2_row->getField('Title'); ?>" href="<?php echo $Title2_row->getField('Pic_location'); ?>"></a>
</div>
<?php }?>

Page will not load unless I echo a string at the top of the page

I have a product.php page that loads a masterpage and a productFocus.php page with additional content. However if I do not echo a string at the top of the productFocus.php page, the page will not load. The masterpage always loads as it should.
Beginning the page with just <?php ?> does not help.
How can I load the content of productFocus.php with the master.php in product.php without the need of echoing a string at the top of the productFocus.php page?
I use JetBeans PhpStorm but XAMPP gives the same result...
product.php
<?php
include_once "db/db.php";
if (isset($_GET["id"])) {
$id = $_GET["id"];
$product = get_product_by_id($id);
$page_content = 'productFocus.php';
include('master.php');
} else {
header("Location: http://localhost/site/index.php");
exit();
}
productFocus.php
<?php echo "." ?> <<<<<<<<<<<<<<<<<<<<<<<< If this is removed the page won't load in the product.php page
<style type="text/css">
...
</style>
<div class="product-focus">
<h3><?php echo $product->name ?></h3>
<img src="images/products/<?php echo $product->image ?>">
<div id="description">
<h4>Productinformatie</h4>
<p><?php echo $product->description ?></p>
<h4>Partners</h4>
<table>
<?php
foreach($product->partners_obj as $partner) {
?>
<tr>
<td>
<a href=<?php echo $partner->product_url ?> target="_blank">
<img id="partner" src="images/partners/<?php echo $partner->image ?>">
</a>
</td>
<td>
€ <?php echo $partner->product_price ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
master.php
<html>
...
<?php include($page_content);?>
...
</html>

Wrong product URLs magento

I've been working on custom up sells related products script, which is displaying 4 random products.
Problem is:
normal product url is:
/shop/$productname
This sometimes is generating url like: /$productname/
or url like: /catalog/product/view/id/$productID/4/s/$productname/category/$categoryid/
I want to have all my URLs the same so: /shop/$productname
<div class="upsell">
<h2>You might be interested in</h2>
<?php // List mode ?>
<?php $_iterator = 0; ?>
<?php // Grid Mode ?>
<table class="products-grid upsell" id="upsell-product-table">
<tbody>
<tr>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); $_columnCount=4; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php endif ?>
<td>
<?php // Initiate product model
$product = Mage::getModel('catalog/product');
// Load specific product whose tier price want to update
$product ->load($_product->getId()); ?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(170); ?>" width="125" height="125" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<h3 class="product-name"><?php echo $_helper->productAttribute($product, $product->getName(), 'name') ?></h3>
<?php echo $this->getPriceHtml($product, true) ?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
</div>
Try
$this->getUrl('shop').$product->getUrlPath()
instead of
$product->getProductUrl()
for both image and product title.
If your indexes are up to date then
$product->getProductUrl()
Should give you the correct product url.
Do not try to build the url yourself from pieces like the url_key, or url_path and the base url. If you move your website to an other serve you're going to have some issues. Also if you change the config settings for categories in product url you can have problems.

I want to fetch data using ajax properly

I want to retrieve data from database using ajax the data is retrieving successfully but it is not showing properly on page. Maybe it is the issue of if else condition.
Here is my code:
<?php
$query = mysql_query("SELECT * FROM cart_polling WHERE country = '".$_REQUEST['id']."' ORDER BY sort_order ASC") or die(mysql_error());
$record = mysql_num_rows($query);
while($getcontry = mysql_fetch_array($query)){
?>
<div class="Question_Table">
<div class="Question_Title">Q<?php echo $getcontry['sort_order']; ?>-<?php echo $getcontry['question']; ?></div>
<!-- ****** Box1 START ****** -->
<?php
if($getcontry['img1']!=""){
?>
<div class="bg">
<?php
if(isset($_SESSION['id'])){
?>
<div class="img"><center>
<a href="javascript:void(0)" class="op">
<img src="images/pollimg/<?php echo $getcontry['img1']; ?>" width="104" height="102" class="middleimg2" onclick="getValue('<?php echo $_REQUEST['id']; ?>','<?php echo $getcontry['option1']; ?>','<?php echo $getcontry['id']; ?>','1','<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>','<?php echo $getcontry['question']; ?>')" /></a>
</center></div>
<?php } ?>
<div class="Vote_button">
<img src="images/vote_button.png" onclick="sendvar();" align="middle">
<img src="images/view_results.png" onClick="showresult(<?php echo $getcontry['id']; ?>)" align="middle">
</div>
<?php
}
</div>
<?php } ?> // here the while loops end.
I want (<div id="txtHint<?php echo $getcontry['id']; ?>"></div>) this in else condition
and the above code in if condition when I click on image viewresult.png a function call which retrieve data from database. I want the data will show in else condition and the above code including loop shows in if condition.
The txtHint($getcontray['id']) is the id of every record whose value changes dynamically with the help of while loop. And txthint is also saves the response of ajax.
If I am using if condition than while loop is not running in else condition and the id can't pass to AJAX response and the correct data will not show, so how can I do that?
in line: <img src="images/pollimg/
'<?php echo $getcontry['img1'] ?>','<?php echo $getcontry['categoryname'] ?>'
missing 2 semicolon.
'<?php echo $getcontry['img1']; ?>','<?php echo $getcontry['categoryname']; ?>'
UPDATE:
And missing ?> php end syntax at the bottom of your file:
<?php
}
//here is the missing tag:
?>
</div>
<?php } ?> // here the while loops end.

Categories