Jump to content

stuck on if then else


oracle765
Go to solution Solved by oracle765,

Recommended Posts

<?php
session_start();
$apiKey = 'secret';
$trackingId = 'secret';
if(isset($_POST['searchkey']) && $_POST['searchkey']=="Search")
{
    if(isset($_POST['search']) && $_POST['search']!="")
    {
        header('location:list.php?key='.$_POST['search'].'');
        exit();
    }
}
if(isset($_GET['key']) && $_GET['key']=="")
{
    header('location:searchform.php');
    exit();
}

if(isset($_POST['price']) && $_POST['price']=="Go")
{
    if(isset($_POST['minPrice']) && isset($_POST['mixPrice']) && $_POST['minPrice']!="" && $_POST['mixPrice']!="")
    {
	header('location:list.php?key='.$_GET['key'].'&minPrice='.$_POST['minPrice'].'&maxPrice='.$_POST['mixPrice'].'&categryId='.$_SESSION['categoryID'].'');
    }
}


$searchresults = urlencode($_GET['key']);
$attributeValue = urlencode($_GET['attribute']);

if(isset($_GET['page']) && $_GET['page']=="")
{
$page = 1;
}
else
{
    $page = $_GET['page'];
}
if($searchresults!="")
{
//$url = "http://sandbox.api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=".$apiKey."&visitorUserAgent&visitorIPAddress&trackingId=".$trackingId."&keyword=" .$searchresults. "&doSkipping=false&showProductOffers=true";
$url = "http://api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=secret&visitorUserAgent&visitorIPAddress&trackingId=secret6&pageNumber=".$page."&itemsSortType=price&itemsSortOrder=".$_GET['sort']."&keyword=".$searchresults;
}

if($attributeValue !="")
{   
$catId = urlencode($_GET['catId']);
$url = "http://api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=secret&visitorUserAgent&visitorIPAddress&trackingId=secret&pageNumber=".$page."&itemsSortType=price&itemsSortOrder=".$_GET['sort']."&categoryId=".$catId."&attributeValue=".$attributeValue;
//$url = "http://sandbox.api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=78b0db8a-0ee1-4939-a2f9-d3cd95ec0fcc&visitorUserAgent&visitorIPAddress&trackingId=7000610&categoryId=".$catId."&attributeValue=".$attributeValue;
}
if($_GET['minPrice']!="" && $_GET['maxPrice']!="" )
{
    
$url = "http://api.ebaycommercenetwork.com/publisher/3.0/json/GeneralSearch?apiKey=cabc762e-61c5-4e4b-a245-4854164268a5&visitorUserAgent&visitorIPAddress&trackingId=8084586&pageNumber=".$page."&itemsSortType=price&itemsSortOrder=".$_GET['sort']."&categoryId=".$_GET['categryId']."&minPrice=".$_GET['minPrice']."&maxPrice=".$_GET['maxPrice'];

}






$response = file_get_contents($url);
$json_arr = json_decode($response, true);
//echo "<pre>";
//print_r($json_arr);
//exit();
//echo "<pre>";
//print_r($json_arr['categories']['category']);
//exit();

if(count($json_arr['categories']['category']))
{
    
    foreach ($json_arr['categories']['category'] as $category) 
    {
	$c = $category['items']['matchedItemCount'] ;
	
	$count = ceil($c/5) ;
	
        $i=0;
        $product = array();
	foreach($category['items']['item'] as $item)
	{
	    
	    //echo "<br>---------------------------------------------------";
	    //echo "<pre>";
	    //print_r($item);
	    //echo "<br>---------------------------------------------------";
	    // get the sub array key
	    $key = key($item);
	    $product[$i]['name'] = $item[$key]['name'];
	    $product[$i]['manufacturer'] = $item[$key]['manufacturer'];
	    
	    $descriptionKey = ($key == 'offer') ? 'description' : 'fullDescription';
	    $product[$i]['description'] = $item[$key][$descriptionKey];
	    
	    $imageListKey = ($key == 'offer') ? 'imageList' : 'images';
	    $product[$i]['image'] = $item[$key][$imageListKey]['image'][0]['sourceURL'];
	    
	    $product[$i]['stockStatus'] = $item[$key]['stockStatus'];
	    $product[$i]['currency'] = $item[$key]['basePrice']['currency'];
	    $product[$i]['basePrice'] = $item[$key]['basePrice']['value'];
	    
	    $product[$i]['minPrice'] = $item['product']['offers']['offer'][0]['basePrice']['value'];
	    $product[$i]['currency2'] = $item['product']['offers']['offer'][0]['basePrice']['currency'];
	    
	    $product[$i]['min'] = $item[$key]['minPrice']['value'];
	    
	    $product[$i]['tax'] =  $item[$key]['tax']['value'];
	    $product[$i]['totalPrice'] = $item[$key]['totalPrice']['value'];
	    $product[$i]['originalPrice'] = $item[$key]['originalPrice']['value'];
	    $product[$i]['offerURL'] = $item[$key]['offerURL'];
	    $product[$i]['id'] = $item[$key]['id'];
	    if($item[$key]['categoryId']!="")
	    {
	    $_SESSION['categoryID'] = $item[$key]['categoryId'];
	    }
	    $i++;
	}
    
    }
    //header('location:searchform.php?msg=No result Found !');
}
else
{
    header('location:searchform.php?msg=1&key='.$_GET['key'].'');
    exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Compare Shopping Prices, Store Prices, Shopping Deals & More | Compare and Choose</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="searchcss/styles.css" type="text/css"  title="default" />
<link href="css/hotelsGadget2013.css" rel="stylesheet" type="text/css" />
<link href="css/Style.ashx.css" rel="stylesheet" type="text/css" />
<link href="css/travel_15072013.css" rel="stylesheet" type="text/css" />
<link href="css/tshCommon.min.css" rel="stylesheet" type="text/css" />
<!--<link href="cool-menu/css/reset.css" rel="stylesheet" type="text/css"/>
<link href="cool-menu/css/960.css" rel="stylesheet" type="text/css"/>
<link href="cool-menu/css/coolMenu.css" rel="stylesheet" type="text/css" media="screen"/>
<script type="text/javascript" src="cool-menu/js/modernizr-1.6.min.js"></script><--->
<link href="mega_dropmenu/css/tshCommon.min.css" rel="stylesheet" type="text/css" />
<script src="mega_dropmenu/js/tshCommon.min.js"></script>
</head>
<body class="list1">
<div class="wrapper">
<div class="page">
<header>
<?php include'menu.php';?>
</header>
<section>
<div class="section">
  <div class="col_right main_colom recbin" style="height: 148px;padding: 0px 10px !important;">
    <h2 style="color: #63a900;">Search for a product</h2><span style="margin-right:0px;">
         <form action="list.php" name="form" method="post">
            <div class="recbin_search" style="padding: 12px 0px 19px 0px !important;width: 948px;">
             <input type="text" class="search" name="search" value="" id="searchbox" style="width: 667px;" placeholder="Enter a product / eg: Apple iphone 3G S or Washing Machine" />
<!--<form action="" method="post">
<input name="search" type="text" placeholder="Enter your mobile phone / tablet or gadget : eg.Apple iphone 3G S" />
<a href="#" class="search_sell">Compare</a>--->
            <input type="submit" name="searchkey" value="Search" class="search_sell search_butt" style="" >
<!--</form>--></div>
         </form>
    <div class="recbin_search">
      <div id="display">
      </div>
    </div>
</div>
<div class="section">
<br>
<div class="col_right main_colom">
  
  
  
<div class="container">
    <div class="catin">
        <div class="top">
            <div class="left_panel hidden-below-desktop">
                <div id="ContentPlaceHolder1_pFilters" class="filters">
                 <div class="filters_wrapper">
		    <?php
		    //echo "<pre>";
		    //print_r($json_arr['categories']['category']);
		    //exit();
		    foreach ($json_arr['categories']['category'] as $category) 
                     {
		       
		       foreach($category['attributes']['attribute'] as $attribute)
                         {
			   if($attribute['name'] == "Price range")
			   {
                    ?>
                     <?php } else { ?>
		     
		     <div class="f-group">
                            <div class="fg-title rokkitt"><?php echo "<a href=".$attribute['attributeURL']."><b>". $attribute['name']."</b></a>"; ?></div>
                            <div class="fg-list">
				<?php foreach($attribute['attributeValues']['attributeValue'] as $attributeValue) { /*$id = end(explode("_",$attributeValue['id']));*/ ?>
                                    <div class="fgl-item"><span class="fgli-name"><a href="list.php?catId=<?php echo $_SESSION['categoryID']; ?>&attribute=<?php echo $attributeValue['id']; ?>&key=<?php echo $_GET['key'] ?>"><?php echo $attributeValue['name'] ; ?></a></span><span class="fgli-amount">(<?php echo $attributeValue['matchingItemsCount'] ; ?>)</span></div>
				<?php } ?>
                            </div>
                        </div>
		     
		     <?php }
		         }
		        } ?>  
                    <div class="f-group">
                        <div class="fg-title rokkitt">Price Range</div>
			<form action="" name="form" method="post">
                        <div class="fg-list">
			<input type="text" name="minPrice">  to  <input type="text" name="mixPrice">   <input type="submit" name="price" value="Go">
                        </div>
			</form>
                    </div>
		    
                 </div>       
               </div>
            </div>
            <div class="main">
            <div class="refine_wrapper visible-tablet-only visible-phone-only visible-phone_wide-only">
		<!--<div class="refine_link">
		    <div class="container">
		<span class="txt"></span>
		<div class="caret-down fix-caret"></div>
		</div>
		</div>--><div class="rp_wrapper"><div class="refine_placeholder"></div></div></div>
            <div class="related_searches"><span class="hidden-below-tablet"></span></div>
            
            <div id="ContentPlaceHolder1_pResults" class="results_wrapper">
	
                <div class="panel">
                    <div class="panel_item term hidden-below-tablet">
		    <?php if(isset($_GET['key']) && $_GET['key']!="")
		    { ?>You searched <span><h1><?php echo $_GET['key']; if($_GET['minPrice']!="" && $_GET['maxPrice']!="" ) echo "(".$_GET['minPrice']."-".$_GET['maxPrice'].")" ;}?>
		    </h1></span></div>
                    <div class="panel_item results_sum hidden-below-tablet">Showing
		    <?php
		    if($_GET['page']=="")
		    {
			$page = 1;
		    }
		    
		    $pr = ($page*5)-4;  echo $pr; ?> - <?php $tr = ($page*5); echo $tr; ?> of <?php echo $c;
		    
		    ?> results</div>
                   <div class="panel_item sort">
                        <span class="sort_txt">Sort: </span>
                        <select name="itemsSortOrder" onchange="" id="itemsSortOrder">
                            <option <?php if($_GET['sort']== "relevance") echo "selected"; ?> value="relevance">Best Match</option>
                            <option <?php if($_GET['sort']== "asc") echo "selected"; ?> value="asc">Price Low-High</option>
                            <option <?php if($_GET['sort']== "desc") echo "selected"; ?> value="desc">Price High-Low</option>
                        </select>
                    </div>
                </div>
                <div class="results_top"></div>
                <div class="results">
                        
			<?php
			//echo "<pre>";
			//print_r($product);
			//exit();
			
			$i = 0;
			foreach($product as $v)
			{
			if(is_numeric($v['id']) && $v['id']>0)   
			{   
			?>
			<div class="list-item li-product  ">
                            <div class="list-item-inner">
                                <div class="pic"><a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlProductImg_0" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>"><img id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_imgProduct_0" title="<?php echo $v['name'];?>" onerror="fixthisimageerror(this,'94')" src="<?php echo $v['image']; ?>" alt="Samsung WF1752WPC Washing Machine"></a></div>
                                <div class="details">
                                    <div class="title"><a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlProductName_0" title="<?php echo $v['name']; ?>" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>"><h2><?php echo $v['name']; ?></h2></a></div>
                                    <div class="desc hidden-below-tablet"><h3><?php echo $v['description']; ?></h3></div>
                                </div>
                                <div class="price">
				    <a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_litPrice_0" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>">
					<span class="from">From<?php echo " ".$v['currency2']; ?></span><span class="from_val"><?php if($v['minPrice']!="") {echo " ".$v['minPrice']; } else { echo $v['min'];}?></span>
				    </a>
				</div>
                                    <div class="details_action">
					<a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlCompareProducts_0" title="<?php echo $v['name']; ?>" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>">
					<span style="float:left;">Compare </span>
					<span class="total"><?php echo $json_arr['categories']['category'][0]['items']['item'][$i]['product']['offers']['matchedOfferCount'] ; ?></span>
					<span class="hidden-below-tablet prices_text"> prices for this product</span>
					</a>
				    </div>
                                <div class="action hidden-below-tablet">
				    <a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlGoToComparePage_0" title="<?php echo $v['name']; ?>" class="_btn2" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>">COMPARE PRICES</a>
				</div>
                            </div>
			</div>
			<?php
			}
			else
		        { ?>
			    
			<div class="list-item li-product  ">
			<div class="list-item-inner">
			    <div class="pic"><a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlProductImg_0" onclick="" href="<?php echo $v['offerURL']; ?>"><img id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_imgProduct_0" title="<?php echo $v['name']; ?>" onerror="fixthisimageerror(this,'94')" src="<?php echo $v['image']; ?>" alt="Samsung WF1752WPC Washing Machine"></a></div>
			    <div class="details">
				<div class="title"><a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlProductName_0" title="<?php echo $v['name']; ?>" onclick="" href="<?php echo $v['offerURL']; ?>"><h2><?php echo $v['name']; ?></h2></a></div>
				<div class="desc hidden-below-tablet"><h3><?php echo $v['description']; ?></h3></div>
			    </div>
			  <div class="price">
			        <a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_litPrice_0" onclick="" href="<?php echo $v['offerURL']; ?>">
				    <span class="from"><?php echo $v['currency']; ?></span><span class="from_val"><?php echo " ".$v['basePrice']; ?></span>
				</a>
			  </div>
				<!--<div class="details_action">-->
				<!--    <a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlCompareProducts_0" title="<?php echo $v['name']; ?>" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>">-->
				<!--    <span style="float:left;">Compare </span>-->
				<!--    <span class="total"><?php echo $json_arr['categories']['category'][0]['items']['item'][$i]['product']['offers']['matchedOfferCount'] ; ?></span>-->
				<!--    <span class="hidden-below-tablet prices_text"> prices for this product</span>-->
				<!--    </a>-->
				<!--</div>-->
			    <div class="action hidden-below-tablet">
                                    <a target="_blank" rel="nofollow" href="<?php echo $v['offerURL']; ?>"><img src="searchcss/go_to_store.png"></a>
			    </div>
			</div>
		    </div>
			<?php }
			$i++ ;} ?>    
                    
                </div>
               <div class="results_bottom"></div>
	       
	       <div class="paging_wrapper">
                    <div id="divPagination" class="paging">
			<?php
			if(strpos($_SERVER['REQUEST_URI'],"page") == true)
			{
			$u = $_SERVER['REQUEST_URI'];
			$t = explode("&page",$u,2);
			$ur = $t[0];
			}
			else
			{
			$ur = $_SERVER['REQUEST_URI'];
			}
			?>
                        <div class="p-item prev">
			    <a id="ContentPlaceHolder1_Prev" rel="prev" href="<?php echo $ur; ?>&page=<?php echo $_GET['page']-1 ; ?>"><?php if($_GET['page']!=1 && $_GET['page']!=""){ echo "Prev Page";} ?></a>
			</div>
                        <div class="p-item">
			    <?php if($_GET['page']) { echo $_GET['page'];} else { echo "1";}?>/<?php echo $count ;?>
			</div>
                        <div class="p-item last next">
			    <a id="ContentPlaceHolder1_Next" rel="next" href="<?php echo $ur; ?>&page=<?php if($_GET['page']==""){ echo "2";}else{ echo $_GET['page']+1 ; } ?>"><?php if($_GET['page']!=$count){ echo "Next Page";}?></a>
			</div>
		    </div>
                </div>         
             </div> 
            </div>
        </div>
    </div>
</div>


</div>
</div>
</section>
<div class="clr"></div>
</div>
<footer>
<?php include'footer.php';?>
<script type="text/javascript">
    //alert(document.URL);
    $(document).ready(
      function()
      {
	    var windowurl = document.URL;
	    var lastpart = windowurl.split('?');
	    
	    $('#itemsSortOrder').change(
	        function()
	    	{
	    	    sortvalue=$('#itemsSortOrder').val();
		    
		    var getpart = windowurl.split('&sort=');
		    
		    //alert(getpart);
		    //
		    //window.location="list.php?"+lastpart[1]+"&sort="+sortvalue;
		    
		    if (getpart[1])
		    {
			firstpart = getpart[0].split('?');
			//window.location="list.php?"+firstpart[1]+"&sort="+sortvalue;
			//alert(firstpart[1]);
			window.location="list.php?"+firstpart[1]+"&sort="+sortvalue;
		    }
		    else
		    {
			window.location="list.php?"+lastpart[1]+"&sort="+sortvalue;
		    }
	    	    
	    	}
	    );
	    
      }
    );



</script>

</footer>
<div class="clr"></div>
</div>
</body>
</html>

Hi Professionals

 

I have the following page code which shows number of stores to compare

 

What I want it to do is

 

If there is only one store then

 

echo 1 store

 

else

 

compare (number of stores) prices for this product

 

end

 

as I did not write this code I am a bit stuck.  I can see in the code where it is just a little lost as to what to do

Link to comment
Share on other sites

<div class="details_action">
					<a id="ContentPlaceHolder1_ProdsRepeater_CompareItem_0_hlCompareProducts_0" title="<?php echo $v['name']; ?>" onclick="" href="compare.php?pid=<?php echo $v['id']; ?>">
					<span style="float:left;">Compare </span>
					<span class="total"><?php echo $json_arr['categories']['category'][0]['items']['item'][$i]['product']['offers']['matchedOfferCount'] ; ?></span>
					<span class="hidden-below-tablet prices_text"> prices for this product</span>
					</a>
				    </div>

ok this part

Link to comment
Share on other sites

yes it mentions an if then else statement. So if its 1 store then echo 1 store else Compare 5 or 4 or 3 or 2 or 6 or 7 or a million or however many stores there happens to be.

 

 

The question is can this be done with an if then else.  If its to complex to confuse you then don't worry about it

Link to comment
Share on other sites

Hi mate, I am a newb but will have a crack at it.

 

To define your question you want to:
  • List Stores that have the product with the price of that product?

 

Hi Professionals

 
I have the following page code which shows number of stores to compare
 
What I want it to do is
 
If there is only one store then
 
echo 1 store
 
else
 
compare (number of stores) prices for this product
 
end
 
as I did not write this code I am a bit stuck.  I can see in the code where it is just a little lost as to what to do

 

 
 
Q: If your pulling your data from a database you might want to get the data you need from a SQL Statement?
Q: Cant see any db connection or SQL statements in your code when I briefly skimmed over it, where is the data coming from? Crawling ebay API (ebaycommercenetwork.com)?
Q: Assuming you have all the data in: 
<?php echo $json_arr['categories']['category'][0]['items']['item'][$i]['product']['offers']['matchedOfferCount'] ; ?>
Would the logic be for your IF Statement be more like:

 

// LOOP through the array with a IF Statement pulling records:

 

IF (['product']) => 1) {

   // Show list of stores and product prices

 

} ELSE {

  // No stores have this product

  echo "Out of stock!";

}

 

Reference Loops and Arrays: http://www.php.net/manual/en/control-structures.foreach.php

 

Hope this helps, I am kinda new to all this, but worth a shot.
Edited by Ansego
Link to comment
Share on other sites

  • Solution

hey ansego

 

for a newbie you done pretty well yeah that worked just had to slightly play around with it

 

eg

 

if (['product']) > 1 {

 

compare (numstores) for this product

 

 

}

else {

 

Only 1 store for this product so I put a go directly to store button as there is obviously only 1 store to choose from for that product

 

}

 

and it don't query the database it queries the ebay network yes oh and I had already covered if there were no stores

 

thanks again for your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.