not getting this header to work correctly - php

I am still working on this and I been trying to add in different things but its not working. When i go to the web site the side is all messed up and I don't know why.Here is my code:
<?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 = count($_SESSION['cart']);
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.
When I change the x=>x to $k=>$v nothing happens. I don't understand this at all the count comes up but the sides is all out of whack. Here is the website http://www.elinkswap.com/snorris/header.html I am sure it is something small but I am still a newbie at this.
ok I am editing this maybe for you guys to understand what i am trying to do here it is:
add in how many items are in the cart on the right side that is what this code is suppose to do..

Change this:
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
to this:
foreach($_SESSION['cart'] as $key=>$X)
{
for($i=0;$i<count($X);$i++){
$itemCount+= $X;
}
}
It should work.

Are you sure that your page is interpreted as a php script ? Default extension of the page to be taken into account by the interpreter is .php, maybe .php3 .php5 depending on the version.
Or maybe you're using some url rewriting I don't see.

Related

php variable on separate page not working

Can anyone help on this please. It's driving me crazy!
I have on one page:
foreach($images_not_on_server_unique as $img => $missing){
foreach($test as $m => $n){
foreach($n as $o => $p){
$query1 = "SELECT * FROM $p WHERE adv='$missing'";
$result1 = mysqli_query($conn,$query1) or die(mysqli_error());
$numofrows = mysqli_num_rows($result1);
if($numofrows >= '1'){
$row1 = mysqli_fetch_array($result1);
$errors_images++;
}
}
}
}
echo $errors_images;
which correctly prints out '16'.
On another page I include the page, and then echo the variable from the first page like so:
echo "errors images ".$errors_images;
which should give me '16'. However, I get only 'errors images'.
What am I doing wrong. I have used include many, many times before and it has always worked (but maybe not in a foreach loop). I have tried using $GLOBALS, but to no avail.
Many thanks for any help.
EDIT
The full code for the second page
<?php
include("login/include/session.php");
include("dbconnect/index_new.php");
require("errors/q_errors.php");
include_once("errors/q_missing_images.php");
echo "errors images ".$errors_images;
?>
UPDATE:
I have added
$my_test = '555';
to the first page and echoed it in the second page with
echo "my test ". $my_test;
and it works correctly!
Therefore it must have something to do with the foreach function in the first page.
Either you're including the file incorrectly
include_once 'path/to/file.php';
Or you're calling it incorrectly
echo 'errors images'.$errors_images.'';

Trying to create a simple shopping cart in PHP with sessions

I've got an assignment where we're supposed to create an eCommerce website that pulls products from a database, lists them and allows you to add them to a cart, though only a single quantity of the item is required but it must have other functions as well, which I think I could figure out on my own.
What I'm trying to work on right now is actually adding items to my cart when the user clicks on the "Buy Now" button I've created for each item pulled from the database but I'm so bloody lost. I'm trying to use a session for the cart so everything is erased when the browser is closed.
Here's what I've got for the page that lists the items available:
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM products WHERE id=$id";
$result = mysqli_query($connection, $sql);
echo '<div id="description">';
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)){
echo '<h2>'.$row['Name'].'</h2>';
echo '<p>'.$row['Description'].'</p>';
echo 'Buy Now - $'.$row['Price'].'.00';
}
}else{
echo "There is something wrong.";
}
echo '</div>';
?>
And here's what I've got for the cart:
<?php
require('connection.php');
session_start();
$cart_content = array();
?>
I haven't gotten past creating the bones for the array that will be the cart items. I don't know if I'm having an off-morning or what, but I can't seem to figure out how to add items with the buttons I have created. When I think about doing it, it seems like it should be easy but I just can't figure it out, no matter the tutorials I look at. For now, this is the only thing I need help with because I'm pretty sure I can figure out the rest on my own.
First as you are using an anchor tag to launch the CART processing you need to add the id to the querystring
<?php
session_start();
// show all products in this script
$sql = "SELECT * FROM products";
$result = mysqli_query($connection, $sql);
echo '<div id="description">';
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)){
// stop it showing a product if it has already been selected by user
if ( ! in_array($row['id'], $_SESSION['cart'] ) {
echo '<h2>'.$row['Name'].'</h2>';
echo '<p>'.$row['Description'].'</p>';
echo 'Buy Now - $'.$row['Price'].'.00';
}
}
}else{
echo "There is something wrong.";
}
echo '</div>';
?>
Now when you look for $_GET['id'] in the cart processing script it should exist in $_GET['id']. All you need to do is add it to something in the $_SESSION array that you can later view. Its usful to give that array a sensible name in $_SESSION.
<?php
session_start();
require('connection.php');
// add this id to the cart array in the session
$_SESSION['cart'][] = $_GET['id'];
// re-run the first script
header('Location: xxxx.php'); // sorry dont know what the fist script is called
?>

Display results of a query before query itself

I have this query:
<?php
echo $grand_total; // info on top of page
$getinfo = mysql_query("SELECT * FROM sales");
while($rows = mysql_fetch_assoc($getinfo)){
$price = $rows['price']; // I want this to be display on top of my page before this query
$quantity = $rows['quantity']; // I want this to be display on top of my page before this query
$total = $price * $quantity;
$grand_total += $total;
}
?>
I want to display the result of this query on the top of my page. Thanks in advance :)
Before the eggs hatch, they remain eggs and not become chickens. You can't tell someone hey here is the chicken (already) which will be hatched from this following egg once the due process completes.
TL;DR
That is not possible.
Food for thought
Imagine there was a way you could do that, then why would you want the hen to sit on those eggs and wait for them to hatch? Why would you then execute the query and waste time if you had that data already?
And Oh
The arrangement should be like that. It's like you are writing from bottom to top. I'm working with a sales report that generates a computations. But the reports should be written from bottom to top. – Archie Zineg
Write a new web programming language that will do that.
Now on to a solution
You're looking at the problem and formulating a solution the wrong way. While it is surely possible that you want to fill the lower part of report first and then go from there to top but it does not mean that you cannot fetch the data beforehand. You can easily use a template and fill it up the way you want. You can easily generate/fetch this data from database at the top and not display it anywhere until you reach the point where you want to use it.
Using Javascript will likely work.
<div id="grandtotal">0.00</div>
<?php
$getinfo = mysql_query("SELECT * FROM sales");
while($rows = mysql_fetch_assoc($getinfo)){
$price = $rows['price']; // I want this to be display on top of my page before this query
$quantity = $rows['quantity']; // I want this to be display on top of my page before this query
$total = $price * $quantity;
$grand_total += $total;
}
?>
<script>
// I am using jQuery here. Traditional js will work
$('#grandtotal').html('<?php echo $grand_total; ?>');
</script>
As explained in the comments and the other answer, you cannot echo something prior to actually retrieving it. you can however, do something like this if you will:
<?php
getFName(); //put this line at the top of your page
function getFName(){
$getinfo = mysql_query("SELECT * FROM table");
while($rows = mysql_fetch_assoc($getinfo)){
$fname = $rows['fname']; // I want this to be display on top of my page before this query
$lname = $rows['lname']; // I want this to be display on top of my page before this query
}
echo $fname;
}
?>

Treeview populated from database

I have been trying to figure out how to create a treeview which is populated from a database using various examples I have come across but have not been successful so far.
My database is structured like:
id | parent_id | title | Urgency (Urgency is not implemented yet)
The end result should generate a treeview where the value of urgency dictates the image used for each item.
The code I have been trying to utilise lately is:
<hmtl>
<body>
<?php
function get_children($parent, $level = 1)
{
$result = mysql_query('SELECT * FROM treeview_items WHERE parent_id = '.$parent);
$result2 = mysql_fetch_array($result, MYSQL_ASSOC);
#for avoiding some errors
if(mysql_num_rows($result) > 0)
#start the list
echo '<ul>';
foreach($result2 as $row) {
#print the item, you can also make links out of these
echo '<li>'.$row['title'].'</li>';
#this is similar to our last code
#this function calls it self, so its recursive
get_children($row['id'], $level+1);
}
#close the list
echo '</ul>';
}
mysql_connect('localhost', 'root');
mysql_select_db('test');
$result = mysql_query('SELECT * FROM treeview_items');
$result2 = mysql_fetch_array($result, MYSQL_ASSOC);
#for avoiding some errors
if(mysql_num_rows($result) > 0) {
#start the list
echo '<ul>';
foreach($result2 as $row) {
#print the item, you can also make links out of these
echo '<li>'.$row['title'].'</li>';
#recursive function(made in next step) for getting all the subs by passing
id of main item
get_children($row['id']);
}
#end the list
echo '</ul>';
#some message if the database is empty
}
else echo 'No Items';
#clear the memory
mysql_free_result($result);
?>
</body>
<html>
Basically the code I have doesn't work. What can I try to fix it?
Edit
I altered some of the code to fix a couple of errors
So now what I see is:
1
2
5
Warning: Invalid argument supplied for foreach() in C:\Program Files\EasyPHP-5.3.9\www\test.php on line 13
repeated 100 times until it aborts. Not sure why, because both foreach() in the code are the same, but the one inside the function doesn't work.
The reason the function call is failing is because you have not wrapped that code in PHP tags, but instead you've wrapped them in JS tags.
replace the <script... line with <?php and the </script> line with ?>
you have server side code in php, not client side javascript (judging by the syntax)
Thank you for all the help but Jey managed to solve my problem by basically giving me entirely new code lol
Here is the link to his code on another question:
Categories with sub PHP / MYSQL
Thank you Jey, and everyone else! :)

Drupal+Ubercart: Get product list

Does anyone here knows how I could get the list of products including image paths so I could manually process them without using Views?
You can write your own mysql queries or use Drupal's framework to launch your customer queries. Something like:
<?php
$query = "SELECT * FROM uc_products WHERE YOUR_WHERE_CLAUSE_HERE";
$result=db_query($query);
print mysql_result($result, 0);
// you probably want to loop through the $result array
while ($row = mysql_fetch_row($result)) {
echo "Product Title = " . $row['title_field'];
echo "Image Url = " . $row['image_url'];
}
?>
Other tables related to Ubercart that may help:
uc_product_classes
uc_product_features
Put this code in a block or page or wherever you are trying to do it.

Categories