Jump to content

Search the Community

Showing results for tags 'foreach'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (PhpStorm, VS Code, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

  1. Hello, I have been trying to figure out how to write a certain php query and for the life of me I can not figure it out. I've tried searching on google like a mad man and still can not figure it out. Maybe Im missing something, Im not sure. I came across this site while googlin for answers. The php code is for a wordpress site I am working on. Here is my current code - <?php $custom_terms = get_terms('videoscategory'); $other_custom_terms = get_terms('product_category'); foreach(array($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'product', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'videoscategory', 'field' => 'slug', 'terms' => $custom_term->slug ), array( 'taxonomy' => 'product_category', 'field' => 'slug', 'terms' => $other_custom_term->slug ), ) ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h1 style="margin-top:10px;">'.$custom_term->name.'</h1>'; while($loop->have_posts()) : $loop->the_post(); echo '<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>'; endwhile; } } ?> Im trying to list all the posts that are in both of my custom taxonomies, however when I try using the code nothing shows but when I use 1 taxonomy at a time, with one array, it works perfectly. Someone on gave me some advice but I do not understand it - generate array of your second taxonomy terms and feed it into the query - What exactly is that advice telling me to do? Any help is appreciated. Thanks.
  2. I've been given a function that return all the list of data in an array. Only that function I can update nothing else specially on the database. From all the list of data, by using foreach I can print all the list of data. But my problem now is on the list of data e.g. (name, address, phone_number, zip_code). . How can I make a condition that will only show all the data by the same zip_code or name whatever. e.g. (John, John, John, John) for me it will be easy if i have access on the database, but unfortunately I don't have. Anyone can help me with this?
  3. MFA

    foreach error

    Hi I have made the code below that outputs the contents of a CSV file into a table. The code works fine however on my server host CGI error log, it says PHP Warning: Invalid argument supplied for foreach() in "my_php_file_name" on line 83 do { $array = fgetcsv($filehandle, 65, ","); echo "<tr>"; foreach ($array as $tableformat) { echo "<td>".$tableformat."</td>"; } echo "</tr>"; $nextrow++; } while ($nextrow <= $lines); $lines refers to the number of lines in the CSV file and $nextrow represents the integer value 0. Although it is working, could someone please explain to me why this message appears so that I can correct it and learn from this experience. Thank you.
  4. Hello all, I'm new to PHP and in a bit of a jam. I have a PHP script that scans a specified folder for all the files in it. All of the files are images. It uses a foreach statement to place them all inside of hyperlinks linked to lightbox to make an image gallery. This works perfectly. Now part two of my gallery project is scanning a different file for videos. I figured it would be pretty easy to edit my existing script and conform it to video. I was wrong. Halfway through rewriting the script I realized that every video has two formats. MP4 and OGG. This will cause every video to be displayed in the gallery twice. Does anyone know how to scan a file for videos and place them on a page without making an embeded video for each format? Below is my code. <?php $videos= glob('videos/*'); natcasesort($videos); foreach($videos as $video) { echo '<video controls tabindex="0">'; echo '<source src="'.$video.'" type="video/mp4;" />'; echo '<source src="'.$video." type="video/ogg;" />'; echo '</video>'; } ?>
  5. Hi All, I am having an issue trying to figure out how to turn the below array into a table by grouping it by customer name. I hope this makes sense. Unfortunately I cant do this with SQL because I have to run calculations on date ranges with the SQL results. Array ( [2year] => Array ([internal] => 16) [1year] => Array ([internal] => 16) [less1year] => Array ([CDR] => 4 [internal] => 171 [Other] => 11 [TPI] => 1) ) Into a table that looks like this: CUSTOMER <1 YR 1 YR 2 YR Internal 171 16 16 CDR 4 Other 11 TPI 1 So far here is the code that I have, im thinking im close, but I cant figure out how to get the same customer names to not repeat on a new <TR> but instead not repeat the customer name but show the value for the correct column. Any help is greatly appreciated. $sql = " SELECT per_id as 'Personnel #', request_customer as 'Customer' FROM submissions, requests, personnels, ( SELECT max(end) as maxdate, personnel_id FROM personnel_submission_link, submissions, requests WHERE sid = submissions_id AND request_id = submissions_rid GROUP BY pl_personnel_id ) as end WHERE request_end = end.maxdate AND end.pl_personnel_id = per_id AND per_id = cv_personnel_submission_link.pl_personnel_id AND pl_sid = submissions_id AND request_id = submissions_rid ORDER BY request_customer "; $result_id = mysql_query ($sql) or die(mysql_error()); $pernumc1 = 0; $pernumc2 = 0; $pernumc3 = 0; while($row = mysql_fetch_array($result_id)) { $customers = $row[1]; $customernames[] = $customers; $calctoday = date("Y-m-d"); $sd = start_check_gap($row[0],45); $dateDiff = strtotime($calctoday) - strtotime($sd); $totaldays = floor($dateDiff/(60*60*24)); $data = $dateDiff / 86400; $data = number_format($data, 0, '.', ''); // returns a number of days after calculations against other orders are found if ($data > 548) { $pernum3 = $pernumc3; $pernumc3++; $pernumname[] = "2 YR"; $custnames3[] = $row[1]; } elseif ($data > 365) { $pernum2 = $pernumc2; $pernumc2++; $pernumname[] = "1 YR"; $custnames2[] = $row[1]; } elseif ($data < 365) { $pernum1 = $pernumc1; $pernumc1++; $pernumname[] = "< 1 YR"; $custnames1[] = $row[1]; } } $pernum3[] = $pernum3; $pernum2[] = $pernum2; $pernum1[] = $pernum1; $ppernum1 = $pernum1; $NewArray3 = array( "2year"=>array_count_values($custnames3), "1year"=>array_count_values($custnames2), "less1year"=>array_count_values($custnames1) ); print_r($NewArray3); $NewArray = array( "More2"=>array_count_values($customernames) ); $newArray2 = array_count_values($pernumname); print("<TABLE WIDTH=100% BORDER=1 BORDERCOLOR=#6c9fc6 CELLSPACING=0>"); print ("<TR BGCOLOR=#c2d6e7>"); print("<TH>CUSTOMER</TH>"); foreach ($newArray2 as $key => $value) { $pernumname = $key; $pernumnum = $value; print("<TH>$pernumname</TH>"); } print("</TR>"); foreach ($NewArray3 as $key => $narray) { foreach ($narray as $key => $value) { $customer = $key; print("<TD VALIGN=TOP>$customer</TD>"); $numpersonnel = $value; print("<TD VALIGN=TOP>$numpersonnel</TD>"); print("</TR>"); } } print("</tr>
  6. I'm pretty new to PHP and so far i've written a function to echo the next game in a schedule. Problem is, I want to display "Today" if there is a game today. Currently if today's date = $today, nothing gets displayed . I've tried an elseif statement inside the foreach, but with no results. If anyone could point me in the right direction, that be great. Thanks. function next_game(){ $schedule = array( 'vs. team1 ' => strtotime('17 Dec 2012'), 'vs. team2 ' => strtotime('19 Dec 2012'), '@team3 ' => strtotime('22 Dec 2012'), '@team4 ' => strtotime('4 Jan 2012') ); $today = time();// current timestamp[/background][/size][/font][/color] foreach($schedule as $place => $date){ if($date > $today) { echo date('M dS', $date)."<small>".$place."</small>"; break; } } } Output Dec 23rd<small>@FGC </small>
  7. Hi, this is the first time I have used arrays in php. I have searched around many other posts but can't seem to get my head around it. I am trying to process multiple arrays entered on a form. The number of entries could be different each time. This is the code I have so far: <?php function submitResults($fixture_id, $ht_goals, $at_goals, $et, $h_pens, $a_pens, $postponed) { //Put group selections into arry in MySQL INSERT value format $valuesAry = array(); foreach(array_keys($fixture_id) as $n) { $valuesAry[] = "($fixture_id[$n], $ht_goals[$n], $at_goals[$n], $et[$n], $h_pens[$n], $postponed[$n])"; } $query = "INSERT INTO test_results (fixture_id, ht_goals, at_goals, et, pens, postponed) VALUES ".implode(', ', $valuesAry); $result = mysql_query($query) or die("Query: {$query}<br>Error: ".mysql_error()); return $result; } ?> It currently displays an error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , , , ), (109, , , , , ), (162, , , , , ), (316, , , , , ), (563, , , , , ), (6' at line 2 Any help anyone can give me would be greatly appreciated.
  8. I have a 1 dimensional array from a database output that I need to have in a multidimensional array ($tasks): array 0 => array 'sr_id' => string '4902367' (length=7) 'description' => string 'GLS Jersey Mikes Installation Checklist' (length=39) 'due_date' => string '2012-01-07 23:24:00' (length=19) 'sr_fe_task_id' => string '220958' (length=6) 1 => array 'sr_id' => string '4902367' (length=7) 'description' => string 'Please submit your pictures of the installation area before any work was performed.' (length=83) 'due_date' => string '2012-01-07 23:24:00' (length=19) 'sr_fe_task_id' => string '220959' (length=6) 2 => array 'sr_id' => string '4902367' (length=7) 'description' => string 'Please submit your pictures of the installation area after all work was completed.' (length=82) 'due_date' => string '2012-01-07 23:24:00' (length=19) 'sr_fe_task_id' => string '220960' (length=6) 3 => array 'sr_id' => string '4903303' (length=7) 'description' => string 'Upload pictures of colored patch cables connected to back of router.' (length=116) 'due_date' => string '2012-01-07 23:31:00' (length=19) 'sr_fe_task_id' => string '220961' (length=6) 4 => array 'sr_id' => string '4899664' (length=7) 'description' => string 'Please upload all pictures taken on site.' (length=41) 'due_date' => string '2012-01-17 17:33:00' (length=19) 'sr_fe_task_id' => string '222839' (length=6) Each sr_id may have more than one task under it and each task has a due_date and description under it, but I'm having trouble getting the descriptions and due_dates under each task: ['sr_id'] =>4902367 ['sr_fe_task_id'] => 220958 ['description']=> "GLS Jersey Mikes Installation Checklist" ['due_date']=> '2011-12-08 17:50:00' ['sr_fe_task_id'] => 220959 ['description']=> "Pleease submit your pictures of the installation area." ['due_date']=> '2012-01-07 23:24:00' ['sr_id'] =>4903303 ['sr_fe_task_id'] => 220961 ['description']=> "Upload pictures of colored patch cables connected to back of router." ['due_date']=> '2012-01-07 23:31:00' I've got it to where each sr_id is listed along with all the tasks underneath of them, but can't get it to where each task has the description and due date under it: for ($i=0; $i<COUNT($tasks); $i++){ $task_list[$tasks[$i]['sr_id']]['task_id'][$tasks[$i]['sr_fe_task_id']] =$tasks[$i]['sr_fe_task_id']; foreach ($task_list[$tasks[$i]['sr_id']]['task_id'] as $v){ $task_list[$tasks[$i]['sr_id']]['task_id'][$v][$tasks[$i]['due_date']]; } }
  9. Heloo guys i have a problem wit a script $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'root'; $dbname = 'site'; $con = mysql_connect($dbhost, $dbuser, $dbpass); if (!$con) { die(mysql_error()); } mysql_select_db($dbname, $con); $sql = "REPLACE INTO stiri (id, titlu, continut, link, categorie, data) VALUES ('','$titlu','$text','$link','Liga 1','$data')"; mysql_query($sql); mysql_close($con); This part is in a php foreach part and every time i run the script i get duplicate entrie, how can i solve this. Thx!
  10. I have a table that I want to create into a horizontal menu using lists/CSS in my header. I have the code that creates the menu, I just can't figure out the foreach loops to actually generate the menu. $refs = array(); $list = array(); $result = full_query("SELECT id,title,slug,parent FROM `page`"); while($data = mysql_fetch_assoc($result)) { $thisref = &$refs[$data['id']]; $thisref['parent'] = $data['parent']; $thisref['title'] = unserialize($data['title']); $thisref['slug'] = $data['slug']; if($data['parent'] == 0) { $list[$data['id']] = &$thisref; } else { $refs[$data['parent']]['children'][$data['id']] = &$thisref; } } // can't figure this part out foreach($list as $keys => $var) { foreach($var as $vkey => $vvar) { if(isset($vkey['children'])) { print $vkey; } } } Which, with my current data, results in this array: Array ( [4] => Array ( [children] => Array ( [1] => Array ( [parent] => 4 [title] => Child1 [id] => 1 [slug] => child1 ) ) [parent] => [title] => Parent1 [id] => 4 [slug] => parent1 ) [6] => Array ( [children] => Array ( [2] => Array ( [parent] => 6 [title] => child2 [id] => 2 [slug] => child2 ) [3] => Array ( [parent] => 6 [title] => child3 [id] => 3 [slug] => child3 ) [8] => Array ( [parent] => 6 [title] => child4 [id] => 8 [slug] => child4 ) ) [parent] => [title] => Parent2 [id] => 6 [slug] => parent2 ) [7] => Array ( [parent] => [title] => Parent3 [id] => 7 [slug] => parent3 ) [9] => Array ( [parent] => [title] => Parent4 [id] => 9 [slug] => parent4 ) ) Am trying to get the foreach to do this: Parent1 Child1 Parent2 Child2 Child3 Child4 Parent3 Parent4 But horizontally. Any help is greatly appreciated!
  11. Hi, I have multiple txt files and I would like to loop them all the the same time Since foreach works with only one array, how do i loop and echo 3 arrays ($handle, $handle2 and $handle_date)? Thanks if(isset($_POST['name'])AND(isset($_POST['msg']))){ $name = $_POST['name']; $msg = $_POST['msg']; if(!empty($name)AND(!empty($msg))) { $handle = fopen('1all_names.txt', 'a'); fwrite($handle, $name."\n"); fclose($handle); $handle2 = fopen('1msg.txt', 'a'); fwrite($handle2, $msg."\n"); fclose($handle2); $handle_date = fopen('1date.txt', 'a'); fwrite($handle_date, $date."\n"); fclose($handle_date);
  12. I am trying to create a list with array elements. It should add a comma after every element except for the last. I have the following code: foreach ($srch as $value) { $query .= "`" . $value . "`"; if (next($srch)){ $query .= ","; } } It is adding a comma after everything except for the LAST TWO elements. Can someone help me understand this? I have read the php.net manual about next() and it seems like it should work for this purpose. Also, I realize there are other ways to do this, but right now I would really like to understand next().
  13. Hi I am currenly doing a project where i am required to send some values to secure http via post request..I am using Curl for this and that works fine, what I am stuck on is how to retrieve stored database values and send each one individually in a loop to the specified url in the curl script. Hard to explain but heres an example: e.g I have a number of reports in db (123456, 123654, 456321) and each one will retrieve a report from the url as they are the specified report_ids required. I would like to pull each one from the database using a foreach loop assigning the report number to $reportid variable one at a time, and also when the report is retrieved with each one, fwrite it to file. I can currently get all of the above working but only if i assign the report number in the script rather than assigning it the db value and looping through each one. I suppose my question is how can i not only loop through db values but assign each id to the reportid sent via curl and then do it all over again for the next id... Any help in the right direction would be awesome.!
  14. i have a foreach statement which looks like this foreach(static::$_components as &$field){ ..some code } in "some code" i sometimes add more components to "static::$_components" member if the insertion happens not at the final iteration it works, and the foreach detect & use the last insertion. but if the insertion happens at the last iteration the foreach statement breaks(ends) without detecting & using the last insertion there is any workaround for this issue beside using for loops and count(array). i prefer using foreach due to performance demands for($iterator = 0, $field = ''; null != ($field = empty(static::$_components[$iterator]) ? null : static::$_components[$iterator]); $iterator++){ ...some code } consumes a lot of memory and proccessing time using for($iterator = 0; $iterator < count(static::$_components); $iterator++){ ...some code } is even worse Thanks in advance
  15. I have this array which has been pulled from a MySQL database (did a var_dump) array(2) { [0]=> array( { ["id"]=> string(1) "3" [0]=> string(1) "3" ["title"]=> string(23) "New Venture Coming Soon" [1]=> string(23) "New Venture Coming Soon" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } [1]=> array( { ["id"]=> string(1) "4" [0]=> string(1) "4" ["title"]=> string(22) "Visit To Headley Court" [1]=> string(22) "Visit To Headley Court" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } } I want to loop through the array and display the information but if $not_archived equals to zero then it displays a message. The problem is that it displays the message becuase $not_archived is equal to zero $z = 0; $not_archived = 0; foreach($news as $newsitem) { if($newsitem['archived'] == 'N') { if($z % 2==0) { //<tr class="yellow"> $z++; } else { //<tr class="white"> $z++ } $not_archived++; echo $newsitem['id']; echo $newsitem['title']; } } if($not_archived == 0) { //No active news in database }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.