PHP - Foreach Issue
hi phpfreaks
In short I don't know what I am doing wrong. I have two arrays that preg_match_all then the results from that are placed into variables. The $valuematch is working fine but the $valuepath won't they print_r correctly but when I run the script and look at the source I find the option name shows nothing. Code: [Select] private function generateSelect ($menuString) { $return ='<select>'; preg_match_all('/href="(.*?)"/', $menuString, $path); print_r($path); preg_match_all('/title="(.*?)"/', $menuString, $matches); print_r($matches); foreach ($matches[1] as $key => $valueMatch) { $path[1] = $valuePath; $return .= "<option name='".$valuePath."'>".$valueMatch."</option>"; } $return.='</select>'; $this->selectOption =$return ; } Similar TutorialsHello I'm having problems regarding coding right the following problem One class (students) can have class all together or/and divided (haveDesdobramento) into 2 shifts So in the beginning i'm choosing all class and defined 100 hours Using a foreach because i need to check holidays, school breaks, start and end of school foreach (range( 0, $datediff ) as $day) { $internal_date = date( INTERNAL_FORMAT, strtotime( "{$startDate} + {$day} days" ) ); $this_day = date( INTERNAL_FORMAT, strtotime( $internal_date ) ); $this_month = date( INTERNAL_FORMAT, strtotime( $internal_date ) ); if($haveDesdobramento === "1") { if ((isSegunda( $internal_date )) && !isExcludedDate( $internal_date )) { $cronograma[$this_month][] = $this_day; if($temposSegunda != null) { if($sum>=$horasAll){ $nhoras = $sum - $horasAll; $temposSegunda = $temposSegunda - $nhoras; echo "tSegunda: ".$temposSegunda; } if ($haveDesdobramento === "1" && $haveAllClass === null) { if($totais<=$horasTurnos){ if ($sameDayShifts === "1") { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = $temposSegunda; } else{ $days = floor($horas/$temposSegunda); $hours_remaining = $horas - $days * $temposSegunda; $cronograma[$this_month][] = $hours_remaining; } } } if ($haveDesdobramento === "1" && $haveAllClass === "1") { if($sum<=$horasAll){ if ($sameDayShifts === "1") { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = $temposSegunda; } else { $cronograma[$this_month][] = $temposSegunda; $cronograma[$this_month][] = 0; $total += $temposSegunda; $cronograma[$this_month][] = $total; } } } else{ $days = floor($horas/$temposSegunda); $hours_remaining = $horas - $days * $temposSegunda; $cronograma[$this_month][] = $hours_remaining; } } $sum += $temposSegunda; } }
It works but it stops at 102, not 100 like it should (choosing 3 hours each time) and the array returned gives me this
Any help?
Thanks
2021-06-07 - 3 - 0 - 99 2021-06-14 - 3 - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661 2021-06-21 - - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661 2021-06-28 - - 0 - 102 Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670 Total: 102
I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. I've included the site that has the print_r of the array function. Any help would be appreciated. http://kansasoutlawwrestling.com/ Code: [Select] function getSubMenuPages() { $this->db->select('site_menu_structures.id'); $this->db->from('site_menu_structures'); $this->db->where('site_menu_structures.short_name', 'mainnav'); $query = $this->db->get(); $menu_id = $query->row()->id; $this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, site_menu_structures_links.is_category'); $this->db->from('site_menu_structures_links'); $this->db->where('site_menu_structures_links.menu_structure_id', $menu_id); $query = $this->db->get(); if ($query->num_rows() > 0) { $linksArray = $query->result(); foreach ($linksArray as $key => $link) { if ($link->is_category == 'Yes') { $this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url'); $this->db->from('site_menu_structures_links_children'); $this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id); $query = $this->db->get(); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { $site_content_page_id = $row->site_content_pages_id; $linksArray[$key]->child_links = array(); if ($site_content_page_id != 0) { $this->db->select('site_content_pages.content_page_name, site_content_pages.permalink'); $this->db->from('site_content_pages'); $this->db->where('site_content_pages.id', $site_content_page_id); $query = $this->db->get(); if ($query->num_rows() > 0) { $row = $query->row(); $linksArray[$key]->child_links = array( 'link_name' => $row->content_page_name, 'link_url' => $row->permalink ); } } else { $linksArray[$key]->child_links = array( 'link_name' => $row->link_name, 'link_url' => $row->link_url ); } } } } } } return $linksArray; } Hi I am new to PHP and I am having an issue with a foreach loop combined with an if statement, basically the if statement is getting the data from the first result in the foreach but not getting the second result, I have tried implement a count by using a variable to iterate but it isnt working: Code as follows: foreach(findCustomAlerts($customerid) as $key=>$value){ echo "ID of cat : ".$rifdid = $value['Catid']."<br>"; echo "Visits required per day : ".$visitsneeded= $value['Visits required']."<br>"; } foreach(getTodaysVisits($device_id) as $rfid){ foreach($rfid as $key=> $read){ if($key ==$rifdid && $read < $visitsneeded) { echo"<br>". $key." has not visited enough"; } } } Ouput is :
ID of cat : 5609 Array ( [rfid_id] => Array ( [23641] => 1 [5609] => 3 ) ). -------> this is the array data
----------------------------------------------- How can I get the loop to iterate to the next value?
Edited October 3, 2020 by Barand code block/indents This kind of has me in lala land as this is very strange... I'm going through a list of dates in such format. Code: [Select] [earned] => Array ( [0] => 2010-07-14T11:26:09Z [1] => 2010-07-31T06:35:14Z [2] => 2010-07-14T11:25:55Z [3] => 2010-07-31T06:29:43Z [4] => 2010-07-31T06:31:47Z [5] => 2010-07-14T11:26:17Z ) Everything is setup correctly but the first offset of an array comes back blank. Like so. - 2010-07-14T11:26:09Z 1280558114 - 2010-07-31T06:35:14Z 1279106755 - 2010-07-14T11:25:55Z 1280557783 - 2010-07-31T06:29:43Z 1280557907 - 2010-07-31T06:31:47Z 1279106777 - 2010-07-14T11:26:17Z Now if I manually use strtotime on just 2010-07-14T11:26:09Z I get the value I seek (1279106769) My foreach setup... Code: [Select] foreach($trophy['earned'] as $key2=>$value2) { echo strtotime($trophy['earned'][$key2]) . " - " . $trophy['earned'][$key2] . "<br />"; } Any ideas? Let me explain my problem. I have an array with dates and numbers in format ($cronograma) Ex: Array ( [2020-09-21] => Array ( [0] => 2020-09-21 [1] => 2 [2] => 2 [3] => 2 ) [2020-09-28] => Array ( [0] => 2020-09-28 [1] => 2 [2] => 2 [3] => 4 ) Then i have another array with 2 ids (in this case 58,60) ($id) Finally i have a third array with numbers only (in this case 34,34) $tot So what i want is cross information beween them, for example for id 58 I must get dates (first element and last element when $tot = 34) for id 60 I must get dates (first element after $tot =34 and last element of array) Whath i have so far is this foreach ($id as $idPlan) { foreach ($cronograma as $c) { $t1 = 0; foreach ($tot as $d) { $t1 += (int)$d['tempos']; if ($c[3] == $t1) { $newAr[] =$idPlan; $newAr[] = $c[0]; } } } } My response array(8) { [0]=> string(2) "58" [1]=> string(10) "2021-02-01" [2]=> string(2) "58" [3]=> string(10) "2021-06-14" [4]=> string(2) "60" [5]=> string(10) "2021-02-01" [6]=> string(2) "60" [7]=> string(10) "2021-06-14" } null So it's clear that i have all repeated I should have a line like: 58 - 2020-09-21 -2021-02-01 Any help? Hey there folks, Im trying to get a foreach loop working correctly from a json source epg heres my code: https://pastebin.com/FjHj1EF6 Here is the current output, which.. while correct is only the first instance of these values i wanted using the foreach loop, i would love any tips on why my approach failed and to be pointed in the right direction. While it has to be included: $url = "https://mywebsite.fake/mlb.php?id={$arr['games'][0]["gamePk"]}"; this value isn't related to the assistance im asking for. that will work for my purposes later. but for now i just need to understand the issue with the foreach loop. Thanks in advance for your thoughtful response. mosb3rgler This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355772.0 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, "; } } In the code I want to proceed to the next step only after stripslashes and strip_tags are completed. How do I put the code below with if? $stripped = array('name', 'location', 'bio'); foreach ( $_POST as $k => $v ) { if ( in_array($k, $stripped) ) { ${$k} = strip_tags($v); } } foreach ( $_POST as $p => $q ) { if ( in_array($p, $stripped) ) { ${$p} = stripslashes($q); } } Thanks, Ruth. public function update($data, $table, $where) { foreach ($data as $column => $value) { $sql = "UPDATE $table SET $column = $value WHERE $where"; print "table: ".$table." column: ".$column." whe ".$where; mysql_query($sql) or die(mysql_error()); } return true; } $data: $data = array( "username" => "Jack", "email" => "jack.gmail@gmail.com" ); $db->update($data, "users", 'id=1'); Working on this code that updates a column from a class. Everything is fine except for one issue. As of right now $column is set to "Jack" which outputs an error, instead of what it should be as "username". I'm sure it's something really simple, so any assistance is appreciated. Any ideas? Problem area is listed in red. Hi guys, Having a bit of trouble getting this to work. I should say before I start that im a rookie at PHP. Basically this is the code I have {counter start=0 print=false name=competcount} {foreach item=compet from=$person.competition} <img src="./bargraph/bar.php?value={$person.lineup[$compet.id].titular}&max={$club.matchsplayed[$compet.id].nummatchs}&info" /> The problem lies with the $club array not being defined Code: [Select] $club.matchsplayed[$competition.id].nummatchs How do I set up multiple arrays with foreach? Am I even in the right ball park with this? I appear to be having a simple simon day! my foreach is only displaying the last result in the table as opposed to each row $num_rows = mysql_num_rows($query); echo "There are $num_rows records.<br>"; while($row = mysql_fetch_row($query)) foreach ($row as $field) { echo '<div class="results">'; echo $field; echo '<br></div>'; } And the source Code: [Select] <link href="../styles/clientbox.css" rel="stylesheet" type="text/css"> <body><br> <h3>My Services</h3> <div class="text"> You currently have the following services with us: </div> There are 2 records.<br><div class="results">test1<br></div> </body> </html> My table has 2 rows which the 1st one has a package of test and the second a package of test1 Can anyone help? Thanks Hi All, I essentially am trying to figure out how to print out all of the zip codes that are associated with a search that I am doing, but not list the same zip code more than once. My current foreach statement is as follows and prints out the zip code for each property that is associated with the search: <ul> <?php foreach($properties as $property) { ?> <li><?=$property->$zipcode?></li> <?php } ?> </ul> How would I modify this to print out 1 occurrence of each zip code that is in the search, even if, for example 20 properties have the same zip code? Thanks in advance! P.S. - Tried using an array to work with it but pretty sure my logic was incorrect when putting it together and couldn't get past the errors. Anyways, thanks in advance for the help! I'm trying to figure out why its saying Warning: Invalid argument supplied for foreach() in /home/content/y/a/n/yankeefaninkc/html/defiant/efedmanager/forms/booking.php on line 17. I thought I did the foreach loop right so far. $eventid = $_GET['id']; $query = "SELECT CONCAT_WS(' ', eventnames.eventname, events.label) AS event, DATE_FORMAT(events.bookingdate, '%M %d, %Y') AS bookingdate, events.nummatches AS nummatches FROM events, eventnames WHERE events.event_id = eventnames.id AND events.id = '" . $eventid . "'"; $result = mysqli_query ( $dbc, $query ); // Run The Query foreach ($nummatches as $matchnum) { echo "Match # $matchnum"; echo "<select class=dropdown name=matchtype id=matchtype title=Match Type>"; echo "<option value=0>- Select -</option>"; $query = 'SELECT id, matchtypename FROM matchtypes'; $result = mysqli_query ( $dbc, $query ); // Run The Query while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) { print "<option value=\"".$row['id']."\">".$row['matchtypename']."</option>\r"; } } why does wordpress is using foreach rather than while in listing records? common Code: [Select] while ($products = mysql_fetch_object($rs) { ... } but wordpress is using foreach Code: [Select] foreach ($posts as $post) { ... } which is better? because i'm trying to create my own code for database connection. today i programmed my own weight calculator. one thing i am having a problem is with trying to make it so if a weight is over a certain number, then the shipping price will increase by X. shipping prices are charged in 20000 gram blocks, so if I have something that is 22000 grams then that will be two blocks. what i need to do is something like (this is a big guess) Quote $row3 = 50; $weighttotal = 22000; if ($weighttotal > 20000) { foreach (20000) { $row3 += $row3;} } } } can it be done on php?? i have a table that echos 10 entries what i want to do is once it echos 5 records it starts a new table. I dont know how to go about this. Could you guys guide me the right way. Noob question. I've learned so much in the last couple of weeks that I've actually found two ways of doing the same thing! I was wondering which was the better method, if there is any difference. They are being used to print somewhere between 10 and 200 lines of text. Either: $categories_count= (count($categories[1])) -1; for ($u=0; $u < $categories_count; $u++) {echo "<li>".$categories[1][$u]."</li>";} Or: foreach ($categories[1] as $line => $cat) {echo "<li>".$cat."</li>";}; Hi! When I echo $m below, it will show all selected options fine. Instead, I want it to show many I selected. So if I selected 3 in dropdown, I want it to say I selected 3 instead of showing their values. $allmaps=$_POST['maps']; foreach ($allmaps as $m) { echo 'm = '.$m.''; } <select name="maps[]" multiple>'.$maps.'</select> Hi i am trying to pass values from a cart to a database using foreach. This is my code: Code: [Select] $columns = array('order_id', 'order_item_id', 'order_product_name', 'order_product_dimensions', 'order_product_options', 'order_product_quantity', 'order_product_price', 'order_product_subtotal', 'order_product_vat', 'order_product_delivery','order_product_total','order_product_sku','order_product_image_name','order_product_image_type','order_product_image_path','order_product_image_size','order_product_artwork_link','order_product_artwork_name','order_product_artwork_type','order_product_artwork_path','order_product_artwork_size','order_firstname','order_lastname','order_companyname','order_email','order_tel','order_billing_address','order_billing_address2','order_billing_town','order_billing_county','order_billing_postcode','order_shipping_address','order_shipping_address2','order_shipping_town','order_shipping_county','order_shipping_postcode','order_status','order_username'); $data = array_fill_keys($columns, 'NULL'); foreach($data as $key => $value) { $data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_escape_string($_POST[$key])."'"; $query = "INSERT INTO orders (id, order_created_at, ".implode(", ",$columns).") VALUES (null, NOW(), ".implode(",",$data).')'; } Problem is posting 2 or more products into the database. How do i split the values so it enters each product into the database? |