PHP - Php Update Foreach Array Error
Code: [Select]
//Update the ranks if (isset($_POST['update'])){ $ids = implode(",", array_map('intval', $_POST['m'])); $ranks = implode(",", array_map('intval', $_POST['ranks'])); if ($ids < 1) message("Incorrect Data"); if ($ranks < 0) message("Incorrect Data"); foreach ($_POST['ranks'] as $muffins){ $db->query('UPDATE friends set rank = '.$muffins.' WHERE friend_id = '.$_POST['m'].' AND user_id = '.$pun_user['id'].'') or error('Unable to remove users from online list', __FILE__, __LINE__, $db->error()); } redirect("s.php?section=Friends","Thanks, Ranks Updated"); } friend_id = becomes blank? the $muffins work and each rank is showed respectivaly, but how can I join in another array in the loop? I tried another foreach in the loop, but it just made me run double the queries. Any idea? $muffins is working fine and $_POST['m'] is giving no results i need to have 2 dynamic arrays in that forloop, the $ranks and the $ids Similar TutorialsHi, after banging my head against the wall for a while thinking this would be a simple task, I'm discovering that this is more complicated than I thought. Basically what I have is a link table linking together source_id and subject_id. For each subject there are multiple sources associated with each. I had created a basic listing of sources by subject... no problem. I now need a way of having a form to create an ordered list in a user-specified way. In other words, I can currently order by id or alphabetically (subject name lives on a different table), but I need the option of choosing the order in which they display. I added another row to this table called order_by. No problem again, and I can manage all of this in the database, however I want to create a basic form where I can view sources by subject and then enter a number that I can use for sorting. I started off looping through each of the entries and the database (with a where), and creating a foreach like so (with the subject_id being grabbed via GET from the URL on a previous script) Code: [Select] while($row = mysqli_fetch_array($rs)) { //update row order if (isset($_POST['submit'])) { //get variables, and assign order $subject_id = $_GET['subject_id']; $order_by = $_POST['order_by']; $source_id = $row['source_id']; //echo 'Order by entered as ' . $order_by . '<br />'; foreach ($_POST['order_by'] as $order_by) { $qorder = "UPDATE source_subject set order_by = '$order_by' WHERE source_id = '$source_id' AND subject_id = '$subject_id'"; mysqli_query($dbc, $qorder) or die ('could not insert order'); // echo $subject_id . ', ' . $order_by . ', ' . $source_id; // echo '<br />'; } } else { $subject_id = $_GET['subject_id']; $order_by = $row['order_by']; $source_id = $row['source_id']; } And have the line in the form like so: Code: [Select] echo '<input type="text" id="order_by" name="order_by[]" size="1" value="'. $order_by .'"/> (yes I know I didn't escape the input field... it's all stored in an htaccess protected directory; I will clean it up later once I get it to work) This, of course, results in every source_id getting the same "order_by" no matter what I put into each field. I'm thinking that I need to do some sort of foreach where I go through foreach source_id and have it update the "order_by" field for each one, but I must admit I'm not sure how to go about this (the flaws of being self-taught I suppose; I don't have anyone to go to on this). I'm hoping someone here can help? Thanks a ton in advance I'm not sure if the title explains it very well, but here is an image of it. I am trying to figure out how to access the option selected (equal, not equal, etc.), along with its input box, for each column (the checkboxes on the right) selected. The purpose of this is to allow a non-programming administrator user to create a custom query to access information. I can find out which checkboxes are selected no problem, but finding the accompanying drop-down option and input box is difficult for me. How do I do this? If you have a suggestion on how to change it, I'm very open. Thank you in advance! Code: [Select] if(!isset($_POST['submit'])) { echo " <form method='post'> <table> <tr> <td valign='top'>SELECT</td> <td valign='top'> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='fields[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> </tr>"; } echo " </table> </td> <td valign='top'>FROM allocations</td> <td valign='top'>WHERE</td> <td> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='column[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> <td> <select name='operator[]'> <option value='='>equals</option> <option value='<>'>does not equal</option> <option value='>'>greater than</option> <option value='<'>less than</option> <option value='>='>greater than or equal to</option> <option value='<='>less than or equal to</option> <option value='LIKE'>is like</option> </select> </td> <td><input type='text' name='criteria[]'></td> </tr>"; } echo " </table> </td> <td valign='top'><input type='submit' value='Process Query' name='submit' class='submit'></td> </tr> </table> </form>"; } else { echo "<b>Fields to edit:</b> "; foreach ($_POST['fields'] as $fields) { echo $fields,", "; } echo "<br><br>"; echo "<b>Columns to query:</b> "; foreach ($_POST['column'] as $columns) { echo $columns," HERE IS THE SPOT, "; } } I have a checkbox when it is checked i garb its value ---meaning the id i take that id and i would like to use it in my where mysql_query statement then I would like to insert the array from the other foreach one after another with a prov meaning provider here is my code print "<select name='prov[]'>"; print '<option value=""></option>'; mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $query2 = "SELECT * FROM admin"; $result2 = mysql_query($query2) or die(mysql_error()); while($row2 = mysql_fetch_array($result2)) { if ($row2['priv']==prov){print '<option value="'.$row2['user'].'">'.$row2['user'].'</option>';} } print '</select>'; print '</div>'; <?php if (!empty($_POST[submitdeletetic])) { foreach ($_POST["delete"] as $key => $id) foreach ($_POST["prov"] as $prov) { mysql_query("UPDATE tickets SET prov='$prov' WHERE id='".(int)$id."'") or die(mysql_error()); } } ?> Hi there, hope have some help. I have a form where a user select time and days of the week (monday, tuesday..) that he is available. The insert script works fine, it insert and the days of week he checked in the table, the only problme is when i try to update each of them.
I used this code above, but is not working, instead of updating/inserting days that is selected is inserting multi-records with the same day of the week value.
here it is:
foreach($weekDay as $DaysofWeek){ $sql_days = "UPDATE available SET day_time = '$day_time', week_day = '$DaysofWeek' WHERE user_id = '$id_user'"; $update_availability = mysql_query($sql_days); } I have been pulling my hair out for the lasy 3 hours i am trying to update a MySql table but i cant get it too work, i just keep getting MySql error #1064 - You have an error in your SQL syntax; if i just update 1 field it works fine but if i try to update more than 1 field it dosent work, Help Please! <?php $root = $_SERVER['DOCUMENT_ROOT']; require("$root/include/mysqldb.php"); require("$root/include/incpost.php"); $con = mysql_connect("$dbhost","$dbuser","$dbpass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("$dbame", $con); mysql_query("UPDATE Reg_Profile_p SET build='$build' col='$col' size='$size' WHERE uin = '$uinco'"); ?> hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. Basically I am getting a warning saying: Code: [Select] Warning: Invalid argument supplied for foreach() in /home/pybuyinf/public_html/store/ds-admin/generate_links.php on line 48 This is what is on Line 48: Code: [Select] <?php foreach ($products as $product){ ?> Hi Guys, I really hope one of you clever genious's can help me. Below is a code which I derived myself, it basically opens up the amazon website on to the page, scrapes everything in between the body tags and then it's supposed to filter out everything into <p></p> tags. But I'm getting the following error Warning: Invalid argument supplied for foreach() in /var/www/vhosts/...scrapeamazon.php on line 13 <?php $bbc = "http://www.amazon.co.uk/"; preg_match_all('/<body[^>]*>\s*(.*?)\s*<\/body>/is',file_get_contents($bbc), $bbcwebsite); $bodytags = $bbcwebsite[1]; if(empty($bodytags) || $bodytags == NULL) { $bbcbody = "Nothing here 1!"; } else { foreach ($bodytags as $line_num => $link) { $bbcbody = $link . "<br />\n"; preg_match_all('/<p[^>]*>\s*(.*?)\s*<\/p>/is', $bbcbody, $bbcp); $pcontent = $bbcp[1] . "<br />\n"; if(empty($pcontent) || $pcontent == NULL) { $bbcptag = "Nothing here 2!"; } else { foreach ($pcontent as $line_num => $ptag) { $bbcptag .= $ptag . "<br />\n"; } } } } ?> I would really appreciate someone's help and thank you in advance. M I have the following function: public function getData($criteria){ $query = "SELECT * FROM {$this->table} WHERE "; $crit = ""; foreach($criteria as $column => $value){ if($column = 'bind'){ $crit .= "{$value} "; }else{ $crit .= "{$column} = {$value} "; } } echo $crit; } I'm testing it with getData(['test1'=>'test2', 'bind'=>'AND', 'test3'=>'test4']); But instead of getting "test1 = test2 AND test3 = test4" the echo output is "test2 AND test4". I'm probably too close to see whats wrong, what do you see? Hi, I have an array: Code: [Select] Array ( [0] => index.php [1] => Home [2] => page.php?id=23 [3] => Page ) How would I loop through it so I can link the previous value to the next value within the array e.g.: Code: [Select] <a href="index.php">Home</a> <a href="page.php?id=23">Page</a> I have the following foreach statement that gets the error msg's from the array called errors. However for the last key in the array, i would not like the <br /> to be echo'd out. How can I achieve that? Code: [Select] foreach ($errors as $msg) { echo $msg; echo "<br />"; } What I' trying to do is take my array Code: [Select] $stores = array("name"=>"$MktName","address"=>"$address"); And for each each value, insert it into my db Code: [Select] foreach($stores as $k => $v){ mysql_query("INSERT INTO stores (name, address) VALUES('$k', '$v' ) ") or die(mysql_error()); } Now, I'm just learning more about arrays, and this is obviously wrong How do I go about getting the value of "name"? It pulls the value for "address". Thanks in advance Hi guys,
Having trouble trying to show values from my $friend array. Only the first friend code is being displayed in my output.
There are several fields in my mysql associated with the $sql.
Any ideas?
Thanks
$sql = "SELECT * FROM friends WHERE Code = '$code' OR FriendCode = '$code' AND Status ='A'"; $result = mysqli_query($cxn,$sql) or die ("Can't get friends."); $friend = array(); while($row=mysqli_fetch_array($result)) { if($code == $row['FriendCode']) { $friend[] = $row['Code']; } elseif($code == $row['Code']) { $friend[] = $row['FriendCode']; } foreach($friend as $key => $value) { echo $value.'<br />'; } Edited by unistake, 18 June 2014 - 06:13 AM. I got this already, my problem is I can't get all values of my $prog array because of '=>' operator. Is there any way to get rid of this? Code: [Select] foreach ($_POST['course'] as $val =>$course) { $prog = $_POST['program'][$val] ; echo $course; echo $prog; } Hello gurus, I've been pounding my head against the wall on this one. It must be something simple I'm missing. I have a text field that posts ($_POST['case_ids']) a comma separated value such as 10067,10076 When I use explode to break it up it should make it an array. However when I use it in a foreach loop I get an invalid argument error. If I do a check for is_array() it fails the check. If I print the array I get: Code: [Select] Array ( [0] => 10067 [1] => 10076 ) So I thought maybe I get the error because the individual array elements don't have commas between them so I wrote this to put commas in between each element except the last: Code: [Select] $cases = array(); $numItems = count($case_ids);//get count of array $i = 0;//initialize counter foreach($case_ids as $key => $value){ if($i+1 != $numItems) { $cases[$key] = $value.','; }else{ $cases[$key] = $value; }//end if $i+1 $i++; } However this new array still gets the invalid argument error. Are these not valid arrays? Any help would be greatly appreciated. Thanks in advance, Twitch I've never worked with multidimensional arrays. I have developed a calendar app. Right now i have it query the db for every day. So potentially 31 queries. Two part question: Would it be better(more "optimized") to query the db once to get all the events in that month for a certain calendar and put the rows into a multidimensional array and use php to determine which events to put in what days? And if that's the case, how do you cycle through the first key in an array like the following? I'm not even sure if this is the best way to structure the array. The number's represent the id number in the db and i will need to cycle through these. Code: [Select] $events = array ( "1"=>array ( "Bob's Birthday", "2011-05-24 16:30:0", "2011-05-25 12:00:00", ), "2"=>array ( "Day Off", "2011-05-27 08:30:0", "2011-05-28 17:00:00", ), "3"=>array ( "Pay rent", "2011-06-01 08:00:0", "2011-05-01 17:00:00", ) ); //something like this...maybe? foreach($events['first_key'] as $key => $value) { // not a clue how to reference the contents } I am a beginner, so forgive me all to whom this is trivial... I have an array, generated from simpleXML object, trying to parse... The array is not symmetrical & I am having trouble finding the right code to make it work. Help & comments are welcomed.. Here is the raw array with print_r: Code: [Select] Array ( [Area] => Array ( [0] => Array ( [Name] => Basement [Id] => 1 [Room] => Array ( [0] => Array ( [Name] => Equipment [Id] => 1 ) [1] => Array ( [Name] => Theater [Id] => 9 ) [2] => Array ( [Name] => Wine Cellar [Id] => 10 ) ) ) [1] => Array ( [Name] => 1st Floor [Id] => 2 [Room] => Array ( [0] => Array ( [Name] => Kitchen [Id] => 2 ) [1] => Array ( [Name] => Main Hall [Id] => 4 ) [2] => Array ( [Name] => Foyer [Id] => 8 ) [3] => Array ( [Name] => Stone Room [Id] => 14 ) [4] => Array ( [Name] => Garage Foyer [Id] => 16 ) [5] => Array ( [Name] => Dining Room [Id] => 17 ) [6] => Array ( [Name] => Living Room [Id] => 18 ) [7] => Array ( [Name] => Hearth Room [Id] => 19 ) [8] => Array ( [Name] => Office [Id] => 20 ) [9] => Array ( [Name] => Powder Room [Id] => 21 ) ) ) [2] => Array ( [Name] => 2nd Floor [Id] => 3 [Room] => Array ( [0] => Array ( [Name] => Landing [Id] => 7 ) [1] => Array ( [Name] => Master Bedroom [Id] => 11 ) [2] => Array ( [Name] => Nannys Room [Id] => 13 ) [3] => Array ( [Name] => East Kids Bed [Id] => 27 ) [4] => Array ( [Name] => West Kids Bed [Id] => 28 ) [5] => Array ( [Name] => Kids Bath [Id] => 29 ) [6] => Array ( [Name] => Robs Closet [Id] => 31 ) [7] => Array ( [Name] => Kelli's Office [Id] => 32 ) ) ) [3] => Array ( [Name] => 3rd Floor [Id] => 7 [Room] => Array ( [Name] => Bonus room [Id] => 5 ) ) [4] => Array ( [Name] => Outside Areas [Id] => 8 [Room] => Array ( [0] => Array ( [Name] => Veranda [Id] => 22 ) [1] => Array ( [Name] => Back Outdoor [Id] => 23 ) [2] => Array ( [Name] => Christmas Lights [Id] => 33 ) [3] => Array ( [Name] => Front Outdoor [Id] => 34 ) [4] => Array ( [Name] => Garage [Id] => 35 ) [5] => Array ( [Name] => Poolhouse Kitchen [Id] => 24 ) [6] => Array ( [Name] => Poolhouse Utility [Id] => 25 ) ) ) ) ) Here is the code: Code: [Select] foreach($xml2['Area'] as $v){ echo $v['Name'],' <br>'; foreach($v['Room'] as $v2){ echo ' ',$v2['Name'],' <br>'; } } Here is the result: Code: [Select] Basement Equipment Theater Wine Cellar 1st Floor Kitchen Main Hall Foyer Stone Room Garage Foyer Dining Room Living Room Hearth Room Office Powder Room 2nd Floor Landing Master Bedroom Nannys Room East Kids Bed West Kids Bed Kids Bath Robs Closet Kelli's Office 3rd Floor B 5 Outside Areas Veranda Back Outdoor Christmas Lights Front Outdoor Garage Poolhouse Kitchen Poolhouse Utility The problem lies with the "3rd Floor" which should contain "Bonus Room" as the room. I know that this is the only element that has no "Rooms" array, but I can't figure out how to code the trap for it. With this data, the condition could exist anywhere, not just my example, but I am stumped with the non symmetrical array that I am unfamiliar with. Any help? Thanks. --akaweed Hi everybody I am new to arrays, and loops pretty much, so please dont be mean! I am trying to turn a single upload form into a multiple upload form. My logic is I need to use arrays and loop the code for uploading the data from the form to the database (for each of these files, create a thumbnail, resize and rename original, move to folder, create path, and insert all that information into the db) But I have no idea how I've got this in the form (now) .... <pick an album to put the files into> <input type="file" name="image[]" > <input type="file" name="image[]" > and this is the code for the whole upload. It worked for single uploads ... but I'm now at a loss ... The code is abit messy, sorry. I'm fairly sure I need a foreach loop, but I dont know where to put it Really Really hoping you can help. Many thanks in advance Code: [Select] <?php ini_set("memory_limit","120M"); include ("DBCONNECT"); include ("CHECK ADMIN"); if ($admin != 1) { header("location:./login.php"); } else { define ("MAX_SIZE","5000"); define ("WIDTH","150"); define ("HEIGHT","100"); function make_thumb($img_name,$filename,$new_w,$new_h) { $ext=getExtension($img_name); if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); $old_x=imageSX($src_img); $old_y=imageSY($src_img); $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); Imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); imagedestroy($dst_img); imagedestroy($src_img); } function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['submit1']) && !$errors) { $sqlalbum="INSERT INTO album (album_name) VALUES ('$_POST[newalbumname]')"; mysql_query($sqlalbum) or die ('Error, album_query failed : ' . mysql_error()); } If(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; // if it is not empty if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { echo '<h1>Unknown extension! You can only upload jpg, jpeg and png files</h1>'; $errors=1; } else { $size=getimagesize($_FILES['image']['tmp_name']); $sizekb=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $image_name=time().'.'.$extension; $newname="uploads/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { $thumb_name='uploads/thumbs/thumb_'.$image_name; $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} }} If(isset($_POST['Submit']) && !$errors) { $album = $_POST['album']; $query = "INSERT INTO upload (name, path, album_id, thumbpath ) ". "VALUES ('$image_name', '$newname' , '$album', '$thumb_name')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<h1>Thumbnail created Successfully!</h1>"; echo '<img src="'.$thumb_name.'">'; } ?> I want to create a loop foreach [@attributes] I seem to be stuck and cant figure it out. any info would be helpful. Thanks! Code: [Select] Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => 5760861 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => 5335615 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => 6080572 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [ID] => 5178898 ) ) ) |