Jump to content

Search the Community

Showing results for tags 'foreach'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • HTML/XHTML
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • XSLT/XSL-FO
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH

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


Languages

Found 12 results

  1. BrainPill

    three arrays in 1 foreach loop

    I can not fix this script, because the output is not in sync. I have 3 arrays. 1) present column names. 2) prefix (fixed name). 3) suffix (uniqid value). Can someone explain how exactly I should create a foreach loop in a foreach loop with 3 arrays and fetching the field value from the get_result command as the the value it is related to. This would result in: <---->Present Column Name <-----> New Column Name <---->value1 <--------------------------------> value1_abcd <---->value2 <------------------------------> value2_abcd etc. <?php $prefix_arr = array( 'col_a' , 'col_b' ,'col_c' , 'col_d', 'col_e' , 'col_f' , 'col_g' , ) ; $col2 = uniqid(); $suffix = substr($col2, 5, 11); $servername = "localhost"; $username = "userx"; $password = "pass12345"; $dbname = "test_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stmt = $conn->prepare("SHOW COLUMNS FROM table_xyz"); $stmt->execute(); $res = $stmt->get_result(); $pres_colname = $res; // var_dump($prefix_arr); foreach ($prefix_arr as $los_ele){ $prefix = $los_ele; // var_dump($prefix); // var_dump($suffix); $col_new = $prefix."_".$suffix; //var_dump($col_new); foreach ($pres_colname as $val){ $col = $val['Field']; // var_dump($col); ?> <br><input type="text" value =" <?php echo $col; ?>"><input type="text" value ="<?php echo $col_new;?>"> <?php $stmt = $conn->prepare("ALTER TABLE `table_xyz` CHANGE COLUMN `$col` `$col_new` VARCHAR(30) NOT NULL;"); $stmt->execute(); } } //$stmt->close(); $conn->close(); ?> The complicated thing this time is that I need the exact 'Field' value which is an output of get_result, so I can not fit it in.
  2. Hello everyone! Am having problem with the youtube grabber I uploaded in my site. The first time I uploaded it on my host it worked well but later stopped. All I see is but later it disappeared and started working well yesterday night the grabber didn't work at all, when am about to make this post I wish to post the screenshort but going to my site to screenshort it I found out that the grabber has started working normal again. Am tired of all this partial contact in the please because I know that very soon it will stop again. Here is the php script of homepage that it do occur <?php include "conf.php"; include "header.php"; $fetchURI="https://www.googleapis.com/youtube/v3/search?type=video&q=bollywood&key=$api_key∂=snippet&maxResults=24"; $ch=curl_init($fetchURI); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data=curl_exec($ch); curl_close($ch); $datas=json_decode($data); $sdata=""; $fetch_data=$datas->items; foreach($fetch_data as $vid){ $vidId=$vid->id->videoId; $thumb_uri=$vid->snippet->thumbnails->medium->url; $title=$vid->snippet->title; $time=strtotime($vid->snippet->publishedAt); $date=date("jS, F Y", $time); $sdata.=' <div class="v-item"> <div class="pplay"> <a href="'.$base.'/view/'.$vidId.'.html"><span class="fa fa-play"></span></a> </div> <img src="'.$thumb_uri.'"> <a href="'.$base.'/view/'.$vidId.'.html">'.$title.'</a> <div class="time-in"><span class="fa fa-clock-o"></span> '.$date.'</div> </div>'; } ?> <div class="w3-row v-cont" id="fsts"> <div class="w3-row w3-padding w3-margin"> <div class="w3-left"> <h1>Newest Videos</h1> </div> </div> <?=$sdata?> </div> <?php include "footer.php"; ?>
  3. I have a table which is either empty or has 1 record If the table is empty i cant retrieve info from the database and get an error message. my code: $sql = "SELECT id FROM users WHERE id = (SELECT MAX(id)FROM users)"; $result = $conn->query($sql); foreach ( $conn->query($sql) as $row ) { $id = $row['id']; the for each line gives an error message: how to solve this?
  4. WesleyA

    foreach loop with list / array

    My goal is to loop with foreach through two variables. So I found this script; <?php $abc = array('Mark','Laura'); $add = array('cocktails','champagne'); $array = array($abc,$add); foreach ($array as list($arr1, $arr2)) { // echo '<br>'; echo '<br>'; echo $arr1; echo '<br>'; echo $arr2; } But the output is not wat is desired. It says: And I would rather have: Is there any function / command in the php array reference to do this or should other code be used?
  5. WesleyA

    Meaning of this error code

    I want to create a condition for my script. It starts with reading an array, this array can be unlimited, at least 4 up to 40 elements. These elements contain 2 variables. I figured out foreach is a better solution than switch (which I use now) In the next example (simple version of my above purpose) I show what I indicate. <?php $elements = array('Volvo', 'Honda', 'Ford'); ?> <form method="post"> <select name="menudrop"> <?php foreach ($elements as $value) : ?> <option value="<?php echo $value ?>"><?php echo $value ?></option> <?php endforeach; ?> </select> <input type="submit" value="submit" /> </form> <?php if (in_array($_POST['menudrop'], $elements)){ echo 'Clients enjoy eating a ' . $_POST['menudrop']; } ?> The problem is an error. The line with the error is: if (in_array($_POST['menudrop'], $elements)) what exactly does this error mean? The script is running and all things are executed like I intended, but I still get an error message at the screen. Notice: Undefined index:
  6. studiosx

    foreach() error

    I want to export videos, thumbs, duration, etc from my website into a xml format. I got this error: Invalid argument supplied for foreach() $output .= ' <clips>';if (!is_array( $vids )) { $vids = array( ); foreach ($imgs as $img) { $img = explode( '.', $img ); $vids[] = '0' . $img[0] . '.flv'; }}@sort( $vids );@sort( $imgs );$i = 38;foreach ($vids as $v) { $output .= ' <clip>'; $output .= ' <duration>' . $duration . '</duration>'; $output .= ' <width>' . $width . '</width>'; $output .= ' <height>' . $height . '</height>'; $output .= ' <flv>' . $v . '</flv>'; $output .= ' <screens> <screen>' . trim( $imgs[$i] ) . '</screen> </screens>'; $output .= ' </clip>'; ++$i;}$output .= ' </clips>'; Thank you!
  7. bwouters

    php array help

    Dear reader, On my own server I created the following code that is working perfectly: $items = []; foreach ($rows as $row) { $items[] = [ "item" => $row["item"], "brand" => $row["brand"], "number" => $row["number"], "price" => $row["price"], "start" => $row["start"], "end" => $row["end"], "shop" => $row["shop"], "location" => $row["location"], "link" => $row["link"] ]; } However my hosting party is not supporting php 5.4 and therefor I transformed the code in: $items = array(); foreach ($rows as $row) { $items = array("item" => $row["item"], "brand" => $row["brand"], "number" => $row["number"], "price" => $row["price"], "start" => $row["start"], "end" => $row["end"], "shop" => $row["shop"], "location" => $row["location"], "link" => $row["link"] ); } and now I am getting the error msgs: "Uninitialized string offset: 0". Does anyone knows what I am doing wrong? Thanks inadvance for your help!!!
  8. bigmuddyfoot

    php sort for each loop

    Hey everyone here is what i have so far..... <?php$posts = get_posts('numberposts=10&order=DESC&category=68&orderby=post_title');foreach ($posts as $post) : start_wp(); ?><?php echo "<hr>"; ?><?php echo the_event_end_date( $id, $showtime, $dateFormat); ?><?php echo "<hr>"; ?><?php the_title(); ?><?php the_excerpt(); ?><?phpendforeach;?> I need the the_excerpt and the the_title to sort in order from soonest to later. (event happening soonest to the one happening later that year)This code the_event_end_date( $id, $showtime, $dateFormat); displayed the event date in this format....June 16, 2013How can i sort this I am out of ideas???PLz HELP!!!!http://wordpress.org/extend/plugins/the-events-calendar/
  9. madsrh

    Foreach only runs once [solved]

    Hi. Can anyone tell me why this code only adds one menu item to my wordpress nav? I'm sure this could be done with an array, but I couldn't figure out how. add_filter( 'wp_list_pages', 'my_menu_link' );add_filter( 'wp_nav_menu_items', 'my_menu_link' ); function my_menu_link($items) { global $wp_query; $CustomMenuLinks;$CustomMenuLinks["Gmail"] = "www.gmail.com";$CustomMenuLinks["YouTube"] = "www.youtube.com";$CustomMenuLinks["aaaaaaaa"] = "www.sdsaaaaaaaaa.com";$class ='menu-item'; foreach( $CustomMenuLinks as $title=> $url){ $menu_link = '<li class="'.$class.'"><a href="'.$url.'">'.$title.'</a></li>'; $items = $items . $menu_link;return $items; }} //MadsRH
  10. Happy new year all, I am hoping that someone can help me with a problem I'm having. I'm using a mysql foreach query to retrieve certain data from the database. However I'm only able to get one set of information per row. Could you please advise me of where the best place is to start reseaching. If I have not explained myself clearly I can provide more information as required. I look forward to hearing from you guys soon. RegardsMatthias
  11. Haighy

    Quick XML foreach question

    i am parsing through xml and want to know when i am at the end of a given 'Product' object ie when at </Product> he is my code $ParsedXml = simplexml_load_string($response); foreach($ParsedXml->Product as $current)( if (isset($current->name)) ( $name = $current->name, ) ...etc... ) I want to know when i am at the end of that Product object so i can write a line into a dabase before going through to the next one
  12. MrDNA

    Foreach Loop

    Hello, I'm stuck at a piece of code, I'm hoping any one could help me further.Here is the idea and the problem: I want to send a mail with a personal touch. The receivers name etc. Thus I had this idea: Get all the ID's out of the database (mysql).With a foreach loop walk trough it. Foreach ID get the remainder of the needed info from the DB,Put them in place.And mail. Thus every one gets his own name in his inbox. I'm hopelessly stuck at the part where I ONLY get the ID's out of the DB and let the foreach run trough them.The sending works and I DO get the ID's out of the DB and to display them with a While loop. Script: $QueryDB = mysql_query("SELECT * FROM Registration");$ResultDB = mysql_fetch_array($QueryDB);foreach($ResultDB as $PersID){ //1) Get email. //2) Get name. //3) send mail.} (Its a quick and smaller rewrite of what I had before. Before the rage quit that is.I'm not that great of a programmer, but trying to get around and learn more by doing things like this!) Any one got a hint, snippet of code or another way to do it?Thank you in advance.
×