I really don't understand why I'm getting this error or how to fix it!
Variable passed to each() is not an array or object on line-
while(list($key,$value) = each($_FILES['images']['name']))
Full code-
$max_no_img=4; // Maximum number of images value to be set here
$imgs_names = array('CV', 'Research Plan', 'Degree Transcript', 'GRE Scores');
echo "<form method=post action='' enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=left>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>".$imgs_names[$i-1]."</br</td><td>
<input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Submit'></td></tr>";
echo "</form> </table>";
while(list($key,$value) = each($_FILES['images']['name']))
{
echo $key;
echo "<br>";
echo $value;
echo "<br>";
if(!empty($value)){ // this will check if any blank field is entered
$filename =rand(1,100000).$value; // filename stores the value
$filename=str_replace(" ","_",$filename);
$add = "upload/$filename"; // upload directory path is set
copy($_FILES['images']['tmp_name'][$key], $add);
echo $add;
Any ideas?
$_FILES['images']['name] is not an array, $_FILES['images'] is.
So
while (list($key, $object) = each($_FILES['images']))
{
//use $object['name'] or $object['tmp_name']
}
$_FILES['images']['name'] is not an array! It is a value inside the array.
If you want to iterate over the files, you should:
while(list($key,$value) = each($_FILES['images']) {
(...)
}
Related
I need to convert JSON into a list using PHP, Tried code below but cannot make it work
$json=file_get_contents("http://feeds.mse.mk/service/FreeMSEFeeds.svc/ticker/JSON/8BA941D0-D6E6-44BD-8D8B-47FDB7A563FA");
$data = json_decode($json);
if (count($data->stand)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->stand as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
And I want to show list as here (not as a table):
http://prntscr.com/no1479
your all code is right but you can use stand class that is wrong your class is GetTickerJSONResult and so change the class stand to GetTickerJSONResult.
try this modified code..
<?PHP
$set =json_decode($json);
if (count($set->GetTickerJSONResult)) {
echo "<table>";
foreach ($set->GetTickerJSONResult as $idx => $stand) {
echo "<tr>";
echo "<td>$stand->AvgPrice</td>";
echo "<td>$stand->Description </td>";
echo "</tr>";
}
echo "</table>";
}
?>
It does not work because in $data->stand there is nothing.
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
while(list($booknum,$rec)=each($bookrec)){
echo "<tr>";
foreach($rec as $data){
echo "<td>". $data."<br></td>";
foreach($data as $k => $val){
if(array_key_exists($val,$sumbook))
$sumbook[$val]['price']=$sumbook[$val]['price']+$data['price'];
else if($k == 'desc' && $k == 'callno')
$sumbook[$val] = $data;
}
}
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I am finishing this code for next week. Also, I am a student and just beginning in PHP. My problem is that I am trying to output the sum of ['price'] in the given array but it gives me this error:
Invalid argument supplied for foreach
I also tried various ways on solving this problem but I don't really get it. Please help. :) Thanks!!
<?php
$bookrec=array(
'book1'=>array('callno'=>123005,'price'=>number_format(1380,2),'desc'=>'Attack on Titan Anthology'),
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge'),
'book3'=>array('callno'=>123004,'price'=>number_format(598,2),'desc'=>'A Work in Progress'),
'book4'=>array('callno'=>123003,'price'=>number_format(668,2),'desc'=>'The Amazing Book is Not on Fire: The World of Dan and Phil'),
'book5'=>array('callno'=>123002,'price'=>number_format(760,2),'desc'=>'Children of Eden: A Novel')
);
$sumbook=array();
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "<br></tr>";
echo "</table>";
echo "TOTAL AMOUNT: Php ".$sumbook;?>
I think you have lots of looping error just follow this code and also understand the how to iterate the array using foreach
$sumbook=array();
$total =0;
echo "<table>";
echo "<tr><b>
<td>Call Number</td><td>Price</td><td>Book Title</td>
</b></tr>";
foreach($bookrec as $key1=>$rec )
{
echo "<tr>";
foreach($rec as $key2=>$data)
{
echo "<td>". $data."<br></td>";
if($key2="price")
{
$total+=$data;
}
}
echo "</tr><br>";
}
echo "</table>";
echo "TOTAL AMOUNT: Php ".$total;
?>
Your first foreach loop had a wrong variable name with the one you initialized as your array.
foreach($bookrec as $data){
}
For a simple voting system i use a .txt file in which the values are stored.
This is the array of options:
$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];
This is the form:
<form method="post" id="quickpoll">
<table>
<?php
foreach ($quickpolloptions as $key => $value) {
echo "<tr>";
echo "<td>";
echo "<label>$value</label>";
echo "</td>";
echo "<td>";
echo "<input type='radio' name='radiovote' value='$key'><br>";
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="Submit">
</form>
This is how i try to add the values in the .txt file (vote_result.txt)
$result_file = "data/vote_result.txt";
if (file_exists($result_file)) {
$results = explode(',', file_get_contents('vote_result.txt'));
} else {
// start with zeros if you don't have a file yet
$results = array_fill(0, count($quickpolloptions), 0);
}
if (isset($_POST['radiovote'])) {
$results[$_POST['radiovote']]++;
file_put_contents('data/vote_result.txt', implode(',', $results));
}
per example: when choosing the second radio, the content of vote_result.txt looks like this: 0,1,0,0,0 This is correct. But when i vote again, lets say i chosse the 3rd radio, he overwrites the .txt file and creates this: ,1. And it should be this: 0,1,1,0,0
What i am doing wrong?
personally I would use serialize / unserialize for such tasks. You can save an array into a txt file and read it back + change it whenever you like, example:
<?php
//initial array for a new file:
$quickpolloptions = [
'Mozilla' => 0,
'Chrome' => 0,
'Opera' => 0,
'IE' => 0,
'Safari' => 0
];
$votefile = "votes.txt";
//init the file:
if(!file_exists($votefile)){
file_put_contents($votefile, serialize($quickpolloptions));
}
//read the file and convert it back to an array:
$data = unserialize(file_get_contents($votefile));
//example of adding a vote for Mozilla:
$data['Mozilla']++;
//save back the file:
file_put_contents($votefile, serialize($data));
print_r($data);
Check below code gives same output as you want.......
<?php
$quickpolloptions = ['Mozilla', 'Chrome', 'Opera', 'IE', 'Safari'];
$result_file = "vote_result.txt";
if (file_exists($result_file)) {
$results = explode(',', file_get_contents('vote_result.txt'));
} else {
// start with zeros if you don't have a file yet
$results = array_fill(0, count($quickpolloptions), 0);
}
if (isset($_POST['radiovote'])) {
$results[$_POST['radiovote']]++;
file_put_contents('vote_result.txt', implode(',', $results));
}
?>
<form method="post" id="quickpoll">
<table>
<?php
foreach ($quickpolloptions as $key => $value) {
echo "<tr>";
echo "<td>";
echo "<label>$value</label>";
echo "</td>";
echo "<td>";
echo "<input type='radio' name='radiovote' value='$key'><br>";
echo "</td>";
echo "</tr>";
}
?>
</table>
<input type="submit" value="Submit">
</form
I've successfully retrieved car make and car name accordingly but I've no idea on how to retrieve image for that particular car.I tried using nested foreach but didn't work as I expected.Instead of displaying particular image for a car id ,it shows the same image from last folder for all ids.
here's my code :
while($row = mysql_fetch_array($result_name,MYSQL_ASSOC)) {
$car[$row['carMake_id']][] = $row['carName'];
//$car_name_id=$row['carName_id'];
$gallery=$row['gallery'];
//$car_make_id=$row['carMake_id'];
//$car_gallery[$row['carName_id']][]=$row['gallery'];
}
foreach ($car as $carmake => $carname) {
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
?>
//this part displays image.. I want it to display according to the car name ($title)
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $gallery;?>" width="100" height="100"></td>
<?php
}
echo'</tr>';
}
Also how do I retrieve all other information pertaining to a particular car inside the foreach?
Thanks.
EDITED PART:
$gallery[]=$row['gallery'];//inside while loop
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title) {
echo "<td>".$title . "<br/> ";
foreach($gallery as $g)//new foreach to retreive images
{
echo $g;
}
?>
<img src="management/uploads/<?php echo $carmake;?>/<?php echo $g;?>" width="100" height="100"></td>
You need to put gallery into the array.
<?php
while($row=mysql_fetch_array($result_name,MYSQL_ASSOC))
{
$car[$row['carMake_id']][] = $row;
}
foreach ($car as $carmake => $carname)
{
echo "<tr><td style='background-color:#0066cc;'><b>".$carmake ."</b></td></tr><tr>";
foreach ($carname as $title)
{
echo "<td>{$title['carName']}<br/> ";
//this part displays image.. I want it to display according to the car name ($title)
echo "<img src='management/uploads/$carmake/{$title['gallery']}' width='100' height='100'></td>";
}
echo'</tr>';
}
?>
I have a php file that contains some data when checked displays the checked data. How do I increment the variable "$visit" every time it is visited and save it to the text file?
new.php
<html>
<body bgcolor="#99FF00">
<table border="1">
<FORM ACTION="new.php" METHOD="POST">
Enter maximum price <input type="text" name="maximumprice"/>
<p><input type="submit" value="Go" name="Go"/>
</form>
<?
$mPrice = $_POST['maximumprice'];
$file1 = "properties.txt";
$filedata = fopen ($file1, "r");
$array1 = file ($file1);
print "<form action=\"visit.php\" method=\"POST\">";
for ($counter1 = 0; $counter1 < count($array1); $counter1++)
{
$arrLine = $array1[$counter1];
$pCode = getvalue ($arrLine, 0);
$price = getvalue ($arrLine, 1);
$picture = getvalue ($arrLine, 2);
$visit = getvalue ($arrLine, 3);
if ($price < $mPrice)
{
print "<tr>";
print "<td>";
print $pCode. "<br>";
print $price. "<br>";
//print $picture. "<br>";
print $visit. "<br>";
print "<input type=\"checkbox\" name=\"box[]\" value=\"$arrLine\" />";
print "</td>";
print "<td>";
printf("<img src='$picture' width='200' height='150'>");
print "</td>";
print "</tr>";
}
}
print '<input type="submit" name="Visit" value="Visit"/>';
// Move the form outside the for loop
print "</form>";
fclose ($filedata);
function getvalue ($text, $arrNo)
{
$intoarray = explode (",", $text);
return $intoarray[$arrNo];
}
?>
</table>
</body>
</html>
this is the second page, display.php
<html>
<body bgcolor="#99FF00">
<?
foreach ($_POST['box'] as $values)
{
echo "$values <hr/>";
}
?>
</body>
</html>
Step 1: Use a database :-)
No, but really, since you are storing the entire line as the value of the checkbox, you can compare that to the line in the file and update your visit field for matching lines ...
For example, in your processing file:
$filename = "properties.txt";
$data = file($filename, FILE_IGNORE_NEW_LINES);
$checked = $_POST['box'];
foreach($data as $id => $line){
if(in_array($line, $checked)){
//Explode the line into parts
$tmp = explode(',', $line);
//Increment the visit field
$tmp[3]++;
//Put the updated data back in the file data array
$data[$id] = implode(',', $tmp);
//Unset the tmp array
unset($tmp);
}
}
//implode the data back into a text block
$data = implode("\n",$data);
file_put_contents($filename, $data);
This is untested, but should yield what you are looking for ...
As a side note, you do not have to do the fopen call to use the file function. It will open and read the file.
EDIT: Since it appears that visit is the last column in each row and without any flags, the file function will keep the newlines at the end of each line, I added the appropriate flag to the file function.