Undefined index: imagedescription[n] (array problem) - php

I'm trying to get the value of an array but I can't.
I've got this page to upload a variable number of photos and their relative descriptions (to be stored in a DB)
for ($i=1; $i<$val; $i++)
{
echo '<div class="row">';
echo '<div class="col-xl-4 col-lg-4 col-md-6 mt-2">';
echo '<div class="file-field" style="margin-bottom: 10px;">';
echo '<div class="z-depth-1-half mt-3" id="thumb-output'.$i.'"></div>';
echo '<div class="d-flex justify-content-center">';
echo '<div class="custom-file">';
echo '<input type="file" data-index="'.$i.'" name="imageupload[]" class="custom-file-input" aria-describedby="inputGroupFileAddon'.$i.'">';
echo '<label class="custom-file-label" for="inputGroupFile'.$i.'">IMG '.$i.'</label>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="col-xl-8 col-lg-8 col-md-6 mt-4">';
echo '<label for="imagedescription'.$i.'">Description IMG '.$i.'</label>';
echo '<input type="text" name="imagedescription[]" class="form-control mt-2" id="imagedescription'.$i.'" aria-describedby="ImageDescription'.$i.'" style="margin-bottom: 0px;">';
echo '<input type="hidden" name="imgcount" value="'.$i.'">';
echo '</div>';
echo '</div>';
}
and this page that "take" and process the datas.
for($i = 0; $i < $count_tmp_name_array; $i++){
$extension = pathinfo($name_array[$i] , PATHINFO_EXTENSION);
$j=$i+1;
$imgname = $albumtitle."-".$j.".".$extension;
if(move_uploaded_file($tmp_name_array[$i], $target_dir.$imgname)){
$photo_decription = $_POST['imagedescription['.$i.']'];
print_r ($photo_decription);
echo "</br>";
}
Basically what I need to do is to assign the varous values of the array imagedescription[] to the relative image (to be upoaded to the DB).
Something like:
[...]
imagedescription[1] -> imageupload[1];
imagedescription[2] -> imageupload[2];
imagedescription[3] -> imageupload[3];
[etc]
The rest is working good but when I try to print the value of $photo_decription or $_POST['imagedescription['.$i.']'] (either with echo or print_r, it doesn't make difference) I recieve the error "Undefined Index for imagedescription[1]", "Undefined Index for imagedescription[2]", "Undefined Index for imagedescription[3]", etc.

Related

print three pictures + text then a new row in PHP

I am trying to create a news site, where 3 articles (image, date, text) per row are displayed. After that a new should begin.
The code produces the articles as I want them put not the three per row printing. The first row has two articles and the rest just one.
<?php
//get all the pictures and texts
$pictures = glob('uploads/news/pictures/*');
$texts = glob('uploads/news/text/*');
if(!$pictures || !$texts){
$error_msg = 'Es gibt keine News';
echo '<h2 class="mt-2 mb-3 text-white center">'.$error_msg.'</h2>';
return;
}
//sort the pictures and texts by date
array_multisort(array_map('filemtime', $pictures), SORT_NUMERIC, SORT_DESC, $pictures);
array_multisort(array_map('filemtime', $texts), SORT_NUMERIC, SORT_DESC, $texts);
//display one picture with its text and the date within a box, 3 boxes per row
echo '<div class="container">';
$count = 1;
for($i = 0; $i < count($pictures); $i++){
if($count % 3 == 1){
echo '<div class="row">';
}
echo '<div class="col-md-4 col-sm-4 p-3 border rounded shadow-lg mx-3">';
echo '<div class="text-center">';
echo '<img src="'.$pictures[$i].'" class="img-responsive" alt="News Image">';
echo '</div>';
echo '<p class="mt-2 mb-3 form-text text-white">'.date('Y-m-d', filemtime($pictures[$i])).'</p>';
echo '<br>';
echo '<p class="mt-2 mb-3 form-text text-white">'.file_get_contents($texts[$i]).'</p>';
echo '</div>';
if(($count)%3 == 0){
echo '</div>';
}
$count++;
}
echo '</div>';
?>

Check box is toggled when I click on any text

I am dynamically creating the checkbox input fields with text lables, and trying to create a treeview when I click any item in the tree the first check box is always toggling.What can be the possible reason.
if($category->visible==1){
echo '<div class="categorylist level'.$depth.' cat">';
$count=$DB->count_records('course',['category'=>$category->id]);
if($count>0){
echo '<div class="parent">';
if($depth>0){
$indent=10;
for($i=0;$i<=$depth;$i++){
$indent=$indent+$depth;
echo '<div style="margin-left:'.$indent.'px">';
}
}
echo '<input type="checkbox" id="category" name="cat[]" value='.$category->id.'/>';
echo '<label for="category"'.$category->id.'"><span class="">'.$category->name.' ('.$count.')</span></label><br>';
if($depth>0){
for($i=0;$i<=$depth;$i++){
echo '</div>';
}
}
echo '</div>';
}
else{
echo '<div class="parent">';
if($depth>0){
$indent=10;
for($i=0;$i<=$depth;$i++){
$indent=$indent+$depth;
echo '<div style="margin-left:'.$indent.'px">';
}
}
echo '<label for="category"><span class="">'.$category->name.' ('.$count.')</span></label><br>';
if($depth>0){
for($i=0;$i<=$depth;$i++){
echo '</div>';
}
}
echo '</div>';
}
echo '</div>';
}
Just checked this <label for="category"'.$category->id.'"> could be your issue, label with duplicate for attribute can cause this issue, make sure that you have no label with same $category->id

MySQL separate in to DIV elements

I would greatly enjoy knowing how to separate the results of my query based on the "season" using PHP only. The products should be placed in separate divs according to what season they are for. I would also like to do this in the most efficient way possible. I have thought about his for some time and have failed both in a solution and in finding someone else's similar results on the internet. Please ignore the jquery, it is a leftover for something I may or may not use.
<?php
session_start();
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="style/main.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0
/jquery.min.js"></script>
<script type="text/javascript" src="script/jquery.simplyscroll.min.js"></script>
<script type="text/javascript">
(function($) {
$(function() {
$("#scroller").simplyScroll();
});
})(jQuery);
</script>
</head>
<body>
<div id="container">
<div id="header">
<div id="menu">
<ul>
<li>Home</li>
<li>About</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="content">
<?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
//$results = $db->query('SELECT id, name, thumb, description, price, season FROM products
BY season ASC');
foreach($db->query('SELECT id, name, thumb, description, price FROM products BY season
ASC') as $results){
if ($results) {
//output results from database
$last_season = 1;
echo '<div class="products">';
while($obj = $results->fetch_object())
{
if ($last_season != $obj->season){
echo '</div><div class="products">';
}
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">Price '.$currency.$obj->price.' <button
class="add_to_cart">Add To Cart</button></div>';
echo '</div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
$last_season = $obj->season;
}
echo '</div>';
}
}
?>
<div id="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&
return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> Check-out!</span>';
echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&
return_url='.$current_url.'">Empty Cart</a></span>';
}else{
echo 'Your Cart is empty';
}
?>
</div>
</div>
<div id="footer">Footer goes here.</div>
</div>
</body>
</html>
I am getting an error on line 44, which is the start of the "foreach" statement. I am again at a loss. Thank you a ton for your help so far. If I can get this working, with help of course, I will be extremely excited and in your debt.
Here's a loop that will create a new div each time the season changes in a new row. You will need to change the query to order the results by season, e.g.
ORDER BY season ASC
Here's the loop:
if ($results) {
//output results from database
$last_season = 1; //initial value
echo '<div class="season">'; //opens first season div
while($obj = $results->fetch_object()){
if ($last_season != $obj->season){
echo '</div><div class="season">';
}
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="image/'.$obj->product_img_name.'"></div>';
echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>';
echo '<div class="product-desc">'.$obj->product_desc.'</div>';
echo '<div class="product-info">Price '.$currency.$obj->price.' <button class="add_to_cart">Add To Cart</button></div>';
echo '</div>';
echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
$last_season = $obj->season;
}
echo '</div>'; //closes final season div
}
Regarding the line 44 error in the foreach, I believe what you want there instead of a foreach is simply:
$results = $db->query('SELECT id, name, thumb, description, price FROM products BY season
ASC'); //remember to remove the closing bracket of the foreach

Creating row using php loop

I want to create a row if columns greater than 3 using PHP loop because i am using wordpress
My code is here
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
If columns are greater than 3, then it should create a new row like this
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
<div class="row">
<div class="column">column1</div>
</div>
Any help will be highly appreciated.
Thanks in advance
Sure - just use modulus:
<?php
$elements = array('foo', 'bar', 'rab', 'oof');
echo '<div class="row">';
foreach ($elements as $i => $element) {
if ($i > 0 && $i % 3 == 0) {
echo '</div><div class="row">';
}
echo '<div class="column">' . $element . '</div>';
}
echo '</div>';
?>
DEMO
Output:
<div class="row">
<div class="column">foo</div>
<div class="column">bar</div>
<div class="column">rab</div>
</div>
<div class="row">
<div class="column">oof</div>
</div>
You need something like this:
<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
if ($i%3 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="column">column '.($i+1).'</div>';
}
echo '</div>';
?>
WORKING CODE
There is alternative solution with function.
<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
// initialize $output
$output = '';
// use "func_get_args()" to collect all parameters into an array
$params = func_get_args();
// used to make sure 10 columns are filled
$maxCols = 10;
// loop through the parameters
foreach ($params as $item) {
$output .= "<td width='80px' align='center'>$item</td>\n";
$maxCols--;
}
// fill in the rest of the row with empty columns
for ($x = 0; $x < $maxCols; $x++) {
$output .= "<td width='80px'> </td>\n";
}
return $output;
}
// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods',
'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;

Show 2 results by div

I have the following foreach:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246"></div>
</div>
<?php } ?>
But thus appears only a div .album within each div .wrapper. How do I see two divs .album within each div .wrapper?
UPDATE
Guys, found the solution:
<?php
$total = 0;
foreach($result15 as $row15){
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
if($total == 0){
echo '<div class="wrapper">';
}
?>
<div class="album" data-disco="disco<?php echo $id15; ?>">
<img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
</div>
<?php
$total = $total + 1;
if($total % 2 == 0){
echo '</div>';
$total = 0;
}
}
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
<div class="album"><img src="img/..." alt="" width="246" height="246"></div>
</div>
Something like this?
EDIT
I dont understand what you want. But you can do this to get two divs, but you will need to get your image path for the second div image:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
Try this:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
A nice solution could be to use array chunk considering you want to treat the data in 'chunks' of 2 images at a time. This way, if you want to alter how many images appear in a wrapper you only need to change your chunk size.
$chunkSize = 2;
foreach (array_chunk($result15, $chunkSize) as $wrapperImages) {
echo '<div class="wrapper">';
foreach ($wrapperImages as $image) {
$thumb = $image->thumb;
echo '<div class="album"><img src="img/'.$thumb.' alt="" width="246" height="246"></div>';
}
echo '</div>';
}

Categories