Unknown "half" error [closed] - php

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I have problem in my PHP code. I'm trying to make internet shop and Im now in product listing step. My code is so long, so I will show only substantial part.
$result = mysql_query("SELECT name, category, describtion, value FROM products") or die(mysql_error());
if(mysql_num_rows($result) % 2 == 0)
{
while($left = mysql_fetch_array($result, MYSQL_NUM) && $right = mysql_fetch_array($result, MYSQL_NUM))
{
echo '
<div class="sub_items">
<div class="sub_left">
<div class="sub_items_header">
<h1>'.$left[0].'</h1>
<h2>'.$left[1].'</h2>
</div>
<div class="sub_items_image">
<img src="images/'.$left[0].'.png" width="167" height="164" alt="'.$left[0].'" />
</div>
<div class="sub_items_text">
'.$left[2].'
</div>
<div class="sub_items_cartinfo">
<div class="price">
<h2>'.$left[3].'$</h2>
</div>
<div class="addtocart">
<span>Add to Cart</span>
</div>
<div class="clearthis"> </div>
</div>
<div class="clearthis"> </div>
</div>
<div class="sub_right">
<div class="sub_items_header">
<h1>'.$right[0].'</h1>
<h2>'.$right[1].'</h2>
</div>
<div class="sub_items_image">
<img src="images/'.$right[0].'.png" width="175" height="170" alt="'.$right[0].'" />
</div>
<div class="sub_items_text">
'.$right[2].'
</div>
<div class="sub_items_cartinfo">
<div class="price">
<h2>'.$right[3].'$</h2>
</div>
<div class="addtocart">
<span>Add to Cart</span>
</div>
<div class="clearthis"> </div>
</div>
<div class="clearthis"> </div>
</div>
<!-- End of Right Sub Item -->
<div class="clearthis"> </div>
</div>
<div class="h_divider"> </div>
';
}
In the while cycle I'm echoing data. Data is separated to left and right div. I'm not getting any error message, but I have bad output. Look.
The left side not loads, but right yes. Why? Anybody please help me.

= has a higher precedence thaN AND; use AND instead of && or parenthesize the assignments.
Instead of
$left = mysql_fetch_array($result, MYSQL_NUM) && $right = mysql_fetch_array($result, MYSQL_NUM)
either
$left = mysql_fetch_array($result, MYSQL_NUM) AND $right = mysql_fetch_array($result, MYSQL_NUM)
or
($left = mysql_fetch_array($result, MYSQL_NUM)) && ($right = mysql_fetch_array($result, MYSQL_NUM))

Related

Bootstrap 5 Dynamic Accordion looking to only have a single header [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I'm using Bootstrap 5 and when I attempt to input data into the accordion it outputs a new header as Drops for every single data that is filled into the body. I'm looking to have just one header for every listing within that database column.
<div class="accordion">
<?php
$i = 1;
$recipes = "SELECT DISTINCT name FROM recipe WHERE category = 'Drops' ORDER BY name ASC;";
$recipe_run = mysqli_query($conn, $recipes);
while($row = mysqli_fetch_assoc($recipe_run) ) {
?>
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne<?= $i ?>">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne<?= $i ?>" aria-expanded="false" aria-controls="collapseOne<?= $i ?>">
<!-- Title -->
Drops
</button>
</h2>
<div id="collapseOne<?= $i ?>" class="accordion-collapse collapse" aria-labelledby="headingOne<?= $i ?>">
<div class="accordion-body">
<!-- Contents -->
<?=$row['name'];?>
</div>
</div>
</div>
<?php
$i++;
}
?>
One heading with multiple contents
Credit to CBroe for the help
<div class="accordion">
<?php
$i = 1;
$recipes = "SELECT DISTINCT name FROM recipe WHERE category = 'Drops' ORDER BY name ASC;";
$recipe_run = mysqli_query($conn, $recipes);
?>
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne<?= $i ?>">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne<?= $i ?>" aria-expanded="false" aria-controls="collapseOne<?= $i ?>">
<!-- Title -->
Drops
</button>
</h2>
<div id="collapseOne<?= $i ?>" class="accordion-collapse collapse" aria-labelledby="headingOne<?= $i ?>">
<div class="accordion-body">
<?php while($row = mysqli_fetch_assoc($recipe_run) ) { ?>
<!-- Contents -->
<?=$row['name'];?>
<?php
$i++;
}
?>
</div>
</div>
</div>

PHP error: HeredocStart expected, syntax error unexpected end of file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm developing a PHP script for my online store and I'm getting this error when I try to use Heredocs. When I delete the $prodotti variable, everything works fine.
<?php
function show_products(){
global $connection;
$query = "SELECT * FROM prodotto";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
if($num_rows = mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$prodotti = <<<STRINGA_PDT
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="../risorse/IMAGES/{$row['immagine']}" alt="">
<div class="card-body">
<h4 class="card-title">
{$row['nome_prodotto']}
</h4>
<h5>€{$row['prezzo']}</h5>
<p class="card-text">{$row['descr_prodotto']}</p>
</div>
<div class="card-footer text-center">
<button type="button" class="btn btn-primary btn-small">Acquista</button>
Dettagli
</div>
</div>
</div>
STRINGA_PDT;
}
}
}
I think it's very simply a case of making sure the marker for the end of the HEREDOC is at the start of the line
<?php
function show_products(){
global $connection;
$query = "SELECT * FROM prodotto";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
if($num_rows = mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$prodotti = <<<STRINGA_PDT
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="../risorse/IMAGES/{$row['immagine']}" alt="">
<div class="card-body">
<h4 class="card-title">
{$row['nome_prodotto']}
</h4>
<h5>€{$row['prezzo']}</h5>
<p class="card-text">{$row['descr_prodotto']}</p>
</div>
<div class="card-footer text-center">
<button type="button" class="btn btn-primary btn-small">Acquista</button>
Dettagli
</div>
</div>
</div>
STRINGA_PDT;
}
}
}
Anything else will cause PHP to fail to recognise it as a Heredoc

PHP While loop stopped after displaying only one product in MySql database [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This php while loop is intended to display information about products stored in MySql database. The while loop is supposed to display the ENTIRE PRODUCTS in the database in bootstrap cards. "Fabric Used" and "Tailor" for each product are stored in separate tables in the database. The tables are connected with foreign keys. Why did the loop stop after displaying only one product AND how do i fix this?
<div class="row" id="result">
<?php
$query ="SELECT * FROM products, tailor_details";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
?>
<div class="col-md-3 mb-2">
<div class="card-deck">
<div class="card border-secondary">
<img src="<?= $row['Picture'] ?>" class="card-img-top">
<div class="card-body">
<h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
<h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
</h4>
<p>Weight: <?= $row['Weight']; ?><br>Tailor:
<?php
$tailor=$row['Tailor'];
$getTailor="SELECT biz_name
FROM tailor_details
WHERE user_id=$tailor";
$res=$dbcon->query($getTailor);
while($rw = $res->fetch_assoc()){
echo $row['biz_name'];
}
?>
<br>
Fabric Used:
<?php
$fabric=$row['Fabric'];
$getfabric="SELECT FabricName FROM fabrics WHERE FabricsId=$fabric";
$result=$dbcon->query($getfabric);
while($row = $result->fetch_assoc()){
echo $row['FabricName'];
}
?>
</p>
Customize
</div>
</div>
</div>
</div>
<?php
} // endwhile
?>
</div>
“I expected the while loop to display every single product and its details in bootstrap cards, just like seen in ecommerce sites; but the actual output I got is just one product and the loop stopped!.”
See database schema below:
products table
fabrics table
tailor_detals table
Screenshot of output
As a lot of peolpe told you in their comments, you don't need three queries, you can do it just with one query. I have changed your code to use only one query:
<div class="row" id="result">
<?php
$query ="SELECT picture,`Name`,price, biz_name,FabricName FROM products a join tailor_details b on a.Tailor=b.user_id join fabrics c on c.FabricsId=a.Fabric";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
?>
<div class="col-md-3 mb-2">
<div class="card-deck">
<div class="card border-secondary">
<img src="<?= $row['Picture'] ?>" class="card-img-top">
<div class="card-body">
<h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
<h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
</h4>
<p>Weight: <?= $row['Weight']; ?><br>Tailor:
<?php
echo $row['biz_name'];
?>
<br>
Fabric Used:
<?php
echo $row['FabricName'];
?>
</p>
Customize
</div>
</div>
</div>
</div>
<?php
} // endwhile
?>
</div>

Dynamic div change alternatively in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here i am generating dynamic div i want to execute div first and second alternatively in PHP as dynamic values comes.. please help me related this.
here $count=$content_faq['number'];count of the result
<?php
foreach($content_faq['rows'] as $howit){
$count=$content_faq['number'];
$title=$howit->title;
$images=$howit->image;
$description=$howit->description;
for($i=0; $i < $count;$i++)
{
if($i % 2){ //remove quotes around 0
?>
<!-- div first -->
<div id="<?php echo rtrim($title); ?>" style="height: 400px; margin-top: 40px;">
<section >
<div class="container about-intro" >
<div class="col-lg-6">
<div class="image1_work">
<img class="img" height="380px" width="100%" src="<?=SITE_MEDIA?>images/howitwork/<?php echo $images?>" alt="image" >
</div>
</div>
<div class="col-lg-6">
<div class="" data-aos="fade-up">
<?php echo $description; ?>
</div>
</div>
</div>
</section><!-- closing of section is misplaced -->
</div>
<?php } else {?>
<!-- div Second -->
<div id="<?php echo rtrim($title); ?>" style="height: 400px; margin-top: 40px;">
<section >
<div class="container about-intro" >
<div class="col-lg-6">
<div class="" data-aos="fade-up">
<?php echo $description; ?>
</div>
</div>
<div class="col-lg-6">
<div class="image1_work">
<img class="img" height="380px" width="100%" src="<?=SITE_MEDIA?>images/howitwork/<?php echo $images?>" alt="image" >
</div>
</div>
</div>
</section>
</div>
<?php } } } ?>
My if and else condition not working in that case whats wrong there can anyone please help related this..
I want if id =1 then if part div execute, if id=2 cos its divisible and reminder is 0 then else part to be executed.. but i am not getting the same result what i am doing wrong here.
A job for the wonderful modulus operator!
Grab the array id with your foreach loop:
foreach ($content_faq['rows'] as $id=>$howit) {
And then use the modulus to alternate
if ($id % 2)
This will evaluate to 0 and 1 alternately, which we know are false and true.

a single php while one three different classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have main div with three different classes: CLASS1; ClASS2 AND CLASS3. Now I want to use a single while loop in all classes to get three images from database and display.
First class is class1 then two div have same class class2 and then last div has class3. How to do it through loop, please help, Thanks :)
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="" alt="">
</div>
</div>
</div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
while ($row = mysqli_fetch_array($result)) { ?>
Answer updated to reflect changes in the Question
I can see roughly what you are trying to achieve, hope this helps you...
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
$class_number = 1;
$class_two_repeated = FALSE;
while ($row = mysqli_fetch_array($result)):
// This bit ensures the 2nd & 3rd divs have class 'ad2'
if ($class_number == 3 && !$class_two_repeated) {
$class_two_repeated = TRUE;
$class_number--;
}
?>
<div class="col-md-<?= $class_number == 3 ? 5 : 2 ?>">
<div class="ad<?= $class_number++ ?>">
<img src="/path/to/<?= $row['whatever_the_image_field_is_named'] ?>" alt="">
</div>
</div>
<?php
endwhile;
?>
</div>

Categories