I need to check if there is a product code in db else assign a product code starting with 101 and add 0001
I am trying like this
$store_code="101";
$product_code=($store_code(0001));
but I get a blank page ...
The result I need is 1010001
any sugestion is welcome
NOTE: the store code comes from MySQL but I show it now like this for an easier understanding
So if I get this correctly, you first check if the product code is in database, and you have no truble doing that.
//lets say $product_code is your code
//if code not in db:
$product_code .= "0001"; //this will add 0001 at the end of the $product_code variable
or
$product_code = $store_code . "0001";
Related
In the following code when I try to assign $viewAd value to the template file and display result it does not display the accurate results when assigned to the template. However, it displays the accurate desired result on top of the page when I straight echo the $viewAd in the PHP page. I have given the screenshots below.
My PHP Structure goes like this:
$cat = $pdo->prepare("SELECT QUERY HERE");
$cat-> execute();
while($s = $cat->fetch()){
$ads = $pdo->prepare("ANOTHER SELECT QUERY HERE");
$ads-> execute();
$ads_count = $ads->rowCount();
if($ads_count > 0){
$viewAd = "<h4>".$s['pcat_category']."</h4>"; // Echoing Category Name
while($a = $ads->fetch()){
if(isLoggedIn()){
// If logged in display Ads relevant to members
$viewAd .= 'SOME HTML DATA';
foreach($membershipData as $mbs){
$viewAd .= 'EXTENDED HTML DATA';
}
$viewAd .= 'CLOSING HTML DATA';
}else{
// If not logged in display ads relevant to outsiders
$viewAd .= 'SOME HTML DATA';
foreach($membershipData as $mbs){
$viewAd .= 'EXTENDED HTML DATA';
}
$viewAd .= 'CLOSING HTML DATA';
}
}
echo $viewAd; // RETURNS DESIRED RESULT ABOVE THE TEMPLATE ON TOP
}
}
$smarty->assign('viewAds', $viewAd); // ASSIGNED TO TEMPLATE BUT DOES NOT RETURN DESIRED RESULT
Screenshot 1 : Correct Result with straight PHP echo displays each category with ads in it
Screenshot 2 : Incorrect Result when variable assigned to the template displays only the last category with the ads in it
Why am I not getting the same result when I assign the variable to the template? I have also tried the array method where I declared $viewAds = array() before the while loop and later I assigned $viewAds[] = $viewAd. Then in the tpl file I tried to echo the value using foreach loop like this {foreach $viewAds as $vas}{$vas}{/foreach} but that still didn't display each category and ads (the desired result like in the first screenshot). Since, it did not work using array as well I removed it and tried to echo {$viewAds} straight way and with foreach too. No luck, nothing is working. All gives the result as in second screenshot. Displaying only the last category with ad in it. However, since direct echo in PHP file is giving the correct result I am sure that my PHP logic is correct. It's just that I am not being able to assign that result in the template file correctly and display it. What is the error I am making here? Am I missing out on something?
You need to initialize $viewAd before starting the first loop, something like:
$viewAd = "";
Then each category should be appended to $viewAd :
$viewAd .= "<h4>".$s['pcat_category']."</h4>";
This way all categories will be included in your final HTML.
I have a shopping cart in my website that has uses a string for the shopping list. Each product has a 3 digit id, so the string looks something like 031181083 with 3 items.
In php,
session_start();
$numbooks = strlen($_SESSION['bookstrings'])/3;
$books = $_SESSION['cart'];
echo "You have " . $numbooks . " books in your Cart.";
echo $_SESSION['bookstrings'];
echo "<br><br>";
include '/redirect.php';
where $_SESSION['cart'] is the string that gets updated when the user adds a product to the cart.
I use a for loop with
${"book$x"} = str_replace("011", "55USD New Traditions and Encounters ", ${"book$x"});
to display the book information instead of the numbers for the user. However, I am having trouble with deleting part of the string when the user cancels the item.
The user presses
echo 'Cancel';
and redirect.php looks like this:
<?php
echo "aaa";
include 'mycart.php';
$x = $_GET['cancel'];
$booksnow = str_replace(${"bookid$x"}, "", $books);
$_SESSION['bookstrings'] = $booksnow;
echo $_SESSION['bookstrings'];
header("Location: mycart.php");
?>
When I go to redirect.php, $_SESSION['bookstrings'] is successfully updated, but when I redirect back to mycart.php, the variable resets.
I don't mind rewriting a lot of stuff, so is there a better way to code the cart? Using a string was the first thing that came to my mind, so built on top of it, but I feel like it's very inefficient, and I would love to use a simpler way with functionality.
PS: when a page opens, the php part does not show up, and when I refresh, it becomes normal. Any ideas?
I have been searching around for help but can't find anything that will fix my problem
I have manipulated a cart code to serve a different a purpose Im bulging a role-play assistance site www.rptoolkit.com/encounter where you add NPC's to the encounter (Cart) and you can then remove them when needed, the site is online if you want to check
the issue is script works perfectly fine in XXMP hosting on my local host but online live, if i add 4 people to the list and try to delete 1 they all clear and the first line gets duplicated (feel free to test)
below is my script code i have used for the delete item button
<h1>Curent Encounter</h1>
<div class="encounter-table">
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["encounter"]))
{
$cart_items = 0;
foreach ($_SESSION["encounter"] as $cart_itm)
{
$id = $cart_itm["code"];
$results = $mysqli->query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$obj = $results->fetch_object();
echo '<td><span>
<a href="include/encounter_remove.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'"><img src="images/icon_delete.gif" alt="Delete"><
/a></span></td>';
and below is the code I have to remove items from the encounter (cart)
<?php
session_start();
include_once("config.php");
//remove item from encounter
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["encounter"]))
{
$id = $_GET["removep"]; //get the product code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["encounter"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]!=$id){ //item does,t exist in the list
$product[ ] = array('code'=>$cart_itm["code"]);
}
//create a new product list for cart
$_SESSION["encounter"] = $product;
}
//redirect back to original page
header('Location:'.$return_url);
}
?>
I would love any help or advise i can I've to fix it, I don't know if its coming downy to the different PHP versions from my XXMP to the server possibly?
Thanks again,
Trev
Session variable size can be limited by the browser. Depending on how much data is stored in `$_SESSION['encounter'], you may be reaching the limit. Consider only storing a id numbers or something similar instead of more complex objects and/or arrays.
okay, im new in this site also new in php and can't get the logic on this,
i have a product page that shows the name, quantity and an add to cart button in each row of product
i made this just cutted of some code
while($showProducts = mysql_fetch_array($products))
{
$currenQuantity = $showProducts['current_quantity'];
$prodid = $showProducts['product_id'];
echo"<select name='quan'>";
for ($x=0;$x<=$currenQuantity;$x++)
{
if($currenQuantity != 0)
{
echo "<option value=$x> $x </option>";
}
}
echo"</select><br/>";
}
now the problem is every time i tried to get the value by using $_POST['quan'] the value that i always get is the default value 1 even i select a different value of quantity of a certain product, and i'm blanked with ideas.
You can't use the same name for an input/select field in a form.
You have to specify a diffrent name or create an indexed array:
<select name="quan[$prodid]">
You can acces it via
$_POST['quan'][$prodid]
Just a wild guess: has each of your product rows a select named 'quan'?
Then you're getting the last select named 'quan' in your POST data.
Prefix each of the quantities selects with name of the product, or some kind of a (scrambled) id.
Additionally:
If you don't want to have a 0 in your select, don't start the for loop with 0
for ($x=1;$x<=$currenQuantity;$x++)
if the current quantity is 0, the for loop doesn't get executed.
If you have more selectboxes with the same name (quan) in one form, you are getting value of the last one.
You should change the selectbox definition to:
echo"<select name='quan[" . $prodid . "]'>";
then you can access the values using:
$_POST["quan"][$some_product_id]
$_POST["quan"][$another_product_id]
I am stuck on this code. I am making a web page and on the side there is a place for a cart. And with the you should be able to click on an item and add it to cart. Well I am having trouble getting it to add it to cart. Can someone help me understand what I should be doing. I have been working on it for a few days and no matter what I am doing nothing is working. If i get the code to show you have 0 in your cart it wont add anything if i try to put it in the cart.
<h1>Cart Contents?</h1>
<div class="p2">
<?php
// Get all the categories and
// link them to category.php.
// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);
// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Print as a list item.
echo "<li>$fcat</li>\n";
if($_SERVER['PHP_SELF']!="CART FILE"){
echo "<h1>Cart Contents</h1>";
echo "<div class=\"p2\">";
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
echo "You have ".$itemCount." total items in your cart.";
echo "</div>\n";
} // End of while loop.
?>
<h1>Specials?</h1>
<div class="p2">
<p>Maybe place specials or new items or related items here.</p>
</div>
</div>
<div class="content">
Ok here is a link to what the cart should do if you look over to the side it should do what that one is doing.
http://www.programmerskit.com/advPHP/ch5/
Shouldn't
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
just be:
$itemCount = count($_SESSION['cart']);
I can't otherwise figure out what that code is supposed to be doing.
Also, that code that outputs the cart appears to be in a while loop outputting each item category, so you will be displaying the cart multiple times, which I can only assume is not desired functionality.
Also, another poster made a point about the invalid use of X as a constant, which is also a good point.
You've got a bare X used all over the place. While saying
$somevar = X;
would be legitimate if you'd already done define('X', 'somevalue') previously, this next one
foreach($_SESSION['cart'] as X=>X){
is completely invalid. You cannot assign new values to a defined constant, let alone try to assign TWO different values at the same time
foreach($_SESSION['cart'] as $key => $value)
is how that particular bit of code should be.