fetching multiple images from mysql database - php

I have been trying to insert multiple images in mysql database. After using below code, it was inserted in the database along with other fields as array. But now I am unable to fetch array to display images.
if(isset($_POST['submit'])){
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$days = $_POST['days'];
$nights = $_POST['nights'];
$people = $_POST['people'];
$hotel_rating = $_POST['hotel_rating'];
$images_name = $_FILES['images']['name'];
$images_type = $_FILES['images']['type'];
$images_size = $_FILES['images']['size'];
$images_tmp = $_FILES['images']['tmp_name'];
$inclusion = $_POST['inclusion'];
$exclusion = $_POST['exclusion'];
$date = date('d/m/y');
$category = $_POST['category'];
if($title == '' or $category == '' or $description == '' or $price == '' or $days == '' or $nights =='' or $people =='' or $hotel_rating==''
or $images_name == '' or $inclusion =='' or $exclusion== ''){
echo "<script>alert('Please fill all fields!')</script>";
exit();
}
for($i = 0; $i< count($images_tmp)-1; $i++){
if($images_type[$i] == "image/jpeg" or $images_type[$i] == "image/png" or $images_type[$i] == "image/gif"){
if($images_size[$i] <= 150000 && $images_size[$i] >= 100000){
move_uploaded_file($images_tmp[$i], "images1/".$images_name[$i]);
}
else {
echo "<script>alert('Please upload image between 100kb and 150kb only.')</script>";
exit();
}
}
else{
echo "<script>alert('Image type is invalid!')</script>";
exit();
}`
I am using the following code to fetch images.
<img src = "<?php echo $images; ?>>
Can anyone help me that what am I doing wrong? Thanks in advance. Following is the code missing for variable initialization:
if(!isset($_GET['cat'])){
$query = "select * from post order by 1 DESC LIMIT 0,4";
$run = mysqli_query($db, $query);
while($row = mysqli_fetch_array($run)){
$post_id = $row['post_id'];
$title = $row['post_title'];
$description = substr($row['post_desc'], 0, 300);
$price = $row['post_price'];
$days = $row['post_days'];
$nights = $row['post_nights'];
$people = $row['post_people'];
$hotel_rating = $row['post_hotel'];
$images = array('images1/'.$row['post_images']);
$inclusion = $row['post_include'];
$exclusion = $row['post_ninclude'];
$date = $row['post_date']; ?>
<!-- Item -->
<div class="item clearfix rating_5">
<div class="item_image"><img src = "<?php echo $images; ?>" width="500" height="400" style="width:100%; height:400px; object-fit: cover;" alt="Image cannot be displayed"></div>

If the $images variable is array of images, you have to do at least
<?php foreach ($images as $image): ?>
<img src="<?= $image ?>">
<?php endforeach ?>
But in your code is no communication with database, no $images variable initialization, nothing. So only we can do is guess.

regardless for all of the mentioned code but in your img tag you have to close the src attribute.

Related

PHP, using two counters to decide top results of an array

I have a block of PHP code where I'm using two counters (one specifically for a particular key of featured if the value is 1.
So far, the counter works by putting any records where featured == 1 in $cnt1 and everything else in $cnt2. However, this doesn't do anything for my sorting.
How can I make this so that the final array puts all featured ($cnt1) records at the top of the array/the first results?
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$lat = get_post_meta($id,'_awpcp_extra_field[36]',true);
$lon = get_post_meta($id,'_awpcp_extra_field[37]',true);
$area = get_post_meta($id,'_awpcp_extra_field[38]',true);
$amnt = get_post_meta($id,'_awpcp_price',true)/100;
$ttle = get_the_title($id);
$link = get_permalink($id);
$desc = get_the_excerpt($id);
$date = get_the_time('m/d/Y',$id);
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$counter_name = ($is_featured == 1) ? 'cnt1' : 'cnt2';
$ress[$$counter_name]['featured'] = $is_featured;
$ress[$$counter_name]['lat'] = $lat;
$ress[$$counter_name]['lon'] = $lon;
$ress[$$counter_name]['area'] = $area;
$ress[$$counter_name]['amnt'] = $amnt;
$ress[$$counter_name]['ttle'] = $ttle;
$ress[$$counter_name]['link'] = $link;
$ress[$$counter_name]['desc'] = $desc;
$ress[$$counter_name]['date'] = $date;
$ress[$$counter_name]['count'] = $counter_name;
if($imgpath){
$ress[$$counter_name]['imgs'] = $imgpath;
}else{
$ress[$$counter_name]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$$counter_name++;
}
}

Wordpress sql, re-ordering array based on meta value

In my wordpress functions file, I'm getting sql query results of only post IDs, then taking those IDs to create an array that I send to my page.
This works perfectly but I want to re-order it based on one of the values I generate in the array process. I have a value of 'featured' and I want to make it so that any results where featured = 1 are first in the array.
$myresults = $wpdb->get_results( $sqlStatement );
$ress = array();
$cnt = 0;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$ress[$cnt]['featured'] = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
if($imgpath){
$ress[$cnt]['imgs'] = $imgpath;
}else{
$ress[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$cnt++;
}
}
echo json_encode($ress);
}
Is there a way to simply use conditions in order to put those values first in the array?
Using a function get_post_meta in an array is bad practice. I would advise first querying the database to extract data from custom fields.
However, I think there are two ways to answer your question:
Use 2 arrays and merge them together after iteration:
$myresults = $wpdb->get_results( $sqlStatement );
$ress1 = $ress2 = array();
$cnt = 0;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$array_name = !empty($is_featured) ? 'ress1' : 'ress2';
$$array_name[$cnt]['featured'] = $is_featured;
if($imgpath){
$$array_name[$cnt]['imgs'] = $imgpath;
}else{
$$array_name[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$cnt++;
}
}
$ress = array_merge($ress1, $ress2);
echo json_encode($ress);
}
Use 2 counters:
$myresults = $wpdb->get_results( $sqlStatement );
$ress = array();
$cnt1 = 0;
$cnt2 = 1000000;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$counter_name = !empty($is_featured) ? 'cnt1' : 'cnt2';
$ress[$$counter_name]['featured'] = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
if($imgpath){
$ress[$$counter_name]['imgs'] = $imgpath;
}else{
$ress[$$counter_name]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$$counter_name++;
}
}
$ress = array_values( $ress );
echo json_encode($ress);
}

How to concatenate variables php/html?

SO here is my code:
<?php
$image = '';
if (getUserChar($_SESSION['user_login']) == 1) {
$image = 'warrior';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 2) {
$image = 'mage';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
elseif (getUserChar($_SESSION['user_login']) == 3) {
$image = 'archer';
echo '<div class="'.$image.'" stlye="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
}
?>
With that, the class seems to work fine. but with regard to the variables top_pos -- it doesnt.
I suspect your $top_pos and $left_pos variables are not being set or being overwritten somewhere.
<?php
function getUserChar($input) {
return $input;
}
// I suspect you have not set these anywhere or are being over written somewhere
$top_pos = 123;
$left_pos = 321;
$image = '';
$_SESSION['user_login'] = 1;
$input = getUserChar($_SESSION['user_login']);
if ($input == 1) {
$image = 'warrior';
} elseif ($input == 2) {
$image = 'mage';
} elseif ($input == 3) {
$image = 'archer';
}
echo '<div class="'.$image.'" style="top:'.$top_pos.'px;left:'.$left_pos.'px;"></div>';
http://sandbox.onlinephpfunctions.com/code/184e6935fa39e733c145441c6a24be6651f41c25

How to hide categories which is not having any products in magento?

I am showing products categories after clicking on any Brand but i want to hide those categories which do no have any products. I am trying from last 6 hours to find the solution but no solution i have found
here is my code below :
<?php
//Image Adjustment
$config = Mage::getModel('manufacturers/image_config');
$image = Mage::getModel('media/image');
$image->setConfig($config);
//Get Manfacturer Info
$mId = $this->getRequest()->getParam('id');
/****************Category Collection**********************/
$manufacturersTable = Mage::getSingleton('core/resource')->getTableName('manufacturers');
$manufacturersProductsTable = Mage::getSingleton('core/resource')->getTableName('manufacturers_products');
$sqry = "SELECT mp.product_id,mp.manufacturers_id FROM ".$manufacturersTable." m
INNER JOIN ".$manufacturersProductsTable." AS mp ON m.manufacturers_id = mp.manufacturers_id
WHERE m.manufacturers_id = ".$mId;
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $connection->query($sqry);
$prds = $select->fetchAll();
$categoryIds=array();
$i=0;
foreach ($prds as $_manufacturer) {
$productIds= $_manufacturer["product_id"];
$_product=Mage::getModel('catalog/product')->load($productIds);
$categoryIds = array_merge($_product->getCategoryIds(), $categoryIds);
$i++;
}
$product=Mage::getModel('catalog/product')->load($prds[0]['product_id']);
$manufacturer = $_product["manufacturer"];
if(empty($manufacturer))
{
$manufacturer = $product->getManufacturer();
}
?>
<?php if ($mId != 0 ) { ?>
<?php
$model = Mage::getModel('manufacturers/manufacturers')->load($mId);
$x = Mage::helper('manufacturers')->getLogoWidth();
$y = Mage::helper('manufacturers')->getLogoHeight();
$color = Mage::helper('manufacturers')->getLogoBackgroundColor();
if($model["m_logo"] != "") {
$imgPath = Mage::helper('manufacturers')->getResizedUrl($model["m_logo"],$x,$y,$color);
} else {
$imageFile = "manufacturers/files/n/i/no_image_available.jpg";
$imgPath = Mage::helper('manufacturers')->getResizedUrl($imageFile,$x,$y,$color);
}
?>
$rootCatId = 257; // haikal 257
$catids=array();
$cat='';
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentId))
->addAttributeToSort('position');
$class = ($isChild) ? "sub-cat-list" : "cat-list";
foreach ($allCats as $category)
{
$html .= $category->getID().',';
$subcats = $category->getChildren();
if($subcats != ''){
$html .= getTreeCategories($category->getId(), true);
}
}
return $html;
}
$catlistHtml = getTreeCategories($rootCatId, true);
//echo $catlistHtml;
($catlistHtml=explode(',', $catlistHtml));
if(count($_GET)) {
?>
<script>
function abc()
{
window.location = window.location.href.split("?")[0];
}
</script>
<div id="whole-cat" class="whole-cat" style="display:none;">
<?php
}
else{
?>
<div id="whole-cat" class="whole-cat" style="display:block">
<?php
}
global $showleftNav;
if(!$showleftNav)
{
$o = null;
$o .= '<div class="content">';
$_columnCount = 5;
$i = 0;
$j = 0;
foreach ($catlistHtml as $_category) {
// if the category is not match the product categories
if(!in_array($_category, $categoryIds)) continue;
$i++;
$_category = Mage::getModel('catalog/category')->load($_category);
//$_category = $_category->load($catId);
$imgurl=$_category->getImageUrl();
$products_count = Mage::getModel('catalog/category')->load($_category->getId())->getProductCount();
if($products_count==0) continue;
if(empty($imgurl))
{
$imgurl=$this->getSkinUrl('images/small_image.jpg');
}
if ($j++%$_columnCount==0):
$o .= '<ul class="pro-cat">';
endif;
$o .= '<li id="m'.$manufacturer.'" class="categoryName row'.$i.'"><h4>'.$_category->getName().'</h4><img src="'.$imgurl.'" ></li class="totalNumberOfProducts">';
if ($j%$_columnCount==0):
$o .= '</ul>';
endif;
}
echo $o;
?>
<?php
$showleftNav==true;
}
?>
</div>
</div>
</div>
In output i have seen that no category is hide
I have tried almost everything i have tried to get Category id by name so that i can search the category and get its id and apply filter.
My page link
Here is the details explanation about this feature:
http://prattski.com/2011/10/06/magento-module-hide-empty-categories/

How to make this PHP code run more efficiently?

PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
foreach($images as $curimg) {
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
if($counter==1){$ImageView_1 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==2){$ImageView_2 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==3){$ImageView_3 = $AutomaticallyOpenShow.$curimg; }
elseif($counter==4){$ImageView_4 = $AutomaticallyOpenShow.$curimg; }
$counter++;
}
}
HTML code:
<img src="<?php echo ImageView_1 ; ?>" width="500" height="500" />
Thanks for Kurro1 and RaggaMuffin-420 answer.
I finally made ​​the integration
PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {
if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
$counter++;
}
}
HTML code:
<img src="<?php echo $ImageView[????] ; ?>" width="500" height="500" />
How about using an array for storage instead of 4 variables?:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
$ImageView = array();
foreach($images as $curimg) {
// the condition could also be optimized
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
// write result in array at appropriate position
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
}
}
The html code would be like the following:
<img src="<?php echo $ImageView[0]; ?>" width="500" height="500" />
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {
if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {
if($counter >= 1 && $counter <= 4){
$varName = 'ImageView_'.$counter;
$$varName = $AutomaticallyOpenShow.$curimg;
}
$counter++;
}
}
Based on the first code you posted.
PHP code:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$ImageView = Array();
$counter = 0;
foreach($images as $curimg) {
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
}
}
HTML code:
<img src="<?php echo ImageView[0] ; ?>" width="500" height="500" />

Categories