How to modify this php code? [closed] - php

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
in my opencart I have image in products category page.
If I have set image in category show in category if I haven't set image show onather one.
<?php if ($thumb) { ?>
<div class="category-img" style="background: url('<?php echo $thumb; ?>') no-repeat;"></div>
<?php }else{ ?>
<div class="category-img" style="background: url(image/catalog/category/default-thum.jpg) 50% 50% no-repeat;"></div>
<?php } ?>
I want to modify code.
If I have set image then show image,
else if
No showing.
Can someone tell me how to modify code?

Use ternary operator .Check this code:
<div class="category-img" style="background: url('<?php echo ($thumb ?: 'image/catalog/category/default-thum.jpg'); ?>') 50% 50% no-repeat;"></div>
http://php.net/manual/en/language.operators.comparison.php

Related

how to search for something in a database and display it? [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 2 years ago.
Improve this question
I tried this code for displaying an item name, price and a photo from database for searching an item name in a search bar
<?php
try
{
require('connection.php');
$sql2="SELECT * FROM items
where item.item_name like '".$txt1."%'
$result2=$db->query($sql2);
if ($result2->rowCount()!=0) {
foreach ($result2 as $r2) { ?>
<a href="images/<?php echo $r2['item_photo'] ?>" class="fh5co-card-item image-popup">
<figure>
<div class="overlay"><i class="ti-plus"></i></div>
<img src="images/<?php echo $r2['item_photo']?>" alt="Image" class="img-responsive">
</figure>
<div>
<?php echo "<p><span class='price kk'>" ; ?>
<p style="font-family:'georgia';text-align:center;font-size:20px;color:black;"><?php echo ($r2['item_name']); ?></span></p>
<h2 style="font-family:'georgia';text-align:center;font-size:18px;color:grey;"><?php echo ("BD ".$r2['item_price']); ?></h2>
</div>
<?php }
} ?>
The problem is the sql, There is an extra name after where
$sql2="SELECT * FROM items
where item.item_name like '".$txt1."%' ;
There should be only item_name not item.item_name. Change this as:
$sql2="SELECT * FROM items
where item_name like '".$txt1."%';
And your code may be exploited with sql injection. Be carefull for production use this code.

Show image only if jsonObj has a value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
If <?php echo $jsonObj->data->image; ?> has a value, like 1234.jpg I want to show this:
<img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>" />
If <?php echo $jsonObj->data->image; ?> has no value, then <img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>" /> should remain hidden.
Any help please?
Use php if-else statement for that.
<?php
if(!empty($jsonObj->data->image)) {
?>
<img src="https://www.example.com/img/<?php echo $jsonObj->data->image; ?>" />
<?php
}
?>
You can add else part and do whatever you want.

Two DIV elements are not stacking correctly on mobile [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
My CSS and HTML are not working. div2 should render below div1 but it's not working on mobile.
#div1 {
width:100%
}
#div2 {
width:100%;
}
<div id = "main">
<div id = "div1">
</div>
<div id = "div2">
</div>
</div>
div {
display:inline-block
}
#div1 {
width: 100%
}
#div2 {
width: 100%;
}
<div id="main">
<div id="div1">
</div>
<div id="div2">
</div>
</div>
making the divs diplay inline:block should solve the problem
Try this one
#div1,#div2 {
width: 100%
display:inline-block;
vertical-align: top;
}

How to print Table of this format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have following data in mysql, and I want to echo in this format:
Data:
Name,Image URL,Link
I want to print it dynamically like this : http://screensaver.cf/screensavers.php
I don't know user's screen width, still I want it to appear as much as possible in width.
How can I do this in html and PHP?
My code:
<?php
require("config.php");
?>
<div id="main-wrap">
<div class="container">
<div id="main">
<div id="content"><div id='wsite-content' class='wsite-elements wsite-not-footer'>
<div class="paragraph" style="text-align:left;">
<?php
$sql='SELECT * FROM `games` where 1=1';
$data = mysql_query($sql);
echo '<h4 class="result">Result:</h4>';
while($row = mysql_fetch_row($data)){
$table=WHAT TO DO HERE TO MAKE IT LOOK LIKE THAT???????
echo $table;
echo '<br><br>';
}
?>
</div>
</div>
</div>
</div>
</div>
<?php
include("footer.php");
?>
P.S I know mysql is depreciated and I am constantly working to learn Mysqli, as I am in 8th class, I don't have much time.
<style>.a{float:left;}
</style>
>
in while loop use this
<div class = "a">
<?php <img src='".$row['url']."' width='140' height='140'> ?>
</div>

pull Else if from SQL [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 8 years ago.
Improve this question
I need the data to have a different style if the sold = 0 in the mysql table
and when i use the code below the website shows a blank white page
(?=$vehicle-> this is the vehicle reference
and sold is the column withing the sql table
<?php
if (?=$vehicle->sold?!= 1)
{
<div class="foo">
<div class="fboverlay"></div>
<a>
<img src="/media.php?productId=<?=$vehicle->vehicle_id?>&file=<?=$vehicle->main_image?>" />
</a>
</div>
}
else {
<img src="/media.php?productId=<?=$vehicle->vehicle_id?>&file=<?=$vehicle->main_image?>" />
}
?>
You need to learn the PHP from the basics. Learn about operators, PHP and HTML secion, etc..
Anyway, i fixed your code. The condition is if $vehicle->sold is not equal to 1. But i think, (in your OP you mentioned it should be 0) you want this: $vehicle->sold == 0
//Use sytnax like this. See php opeartors.
if ($vehicle->sold != 1) {
?> <!-- Close the php -->
<div class = "foo">
<div class = "fboverlay"></div>
<img src = "/media.php?productId=<?= $vehicle->vehicle_id ?>&file=<?= $vehicle->main_image ?>" />
</div>
<?php
//Open the php again
} else {
?> <!-- close the php -->
<img src = "/media.php?productId=<?= $vehicle->vehicle_id ?>&file=<?= $vehicle->main_image ?>" />
<?php //Open again
}

Categories