Search the Community
Showing results for tags 'foreach'.
-
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 />'; }
-
Hi guys, I am trying to echo multiple calendar events from a mysql table and order them by the date column. There are several events on each day so I am trying to echo the date once then list the associated events under that date. Such as 2014-05-10 Event 1, event 2, event 3 2014-05-11 Event 1, event 2, event 3 instead of 2014-05-10 event 1, 2014-05-10 event 2, 2014-05-10 event 3, 2014-05-11 event 1 etc... This is my coding so far: while($row = mysqli_fetch_assoc($result)) { $dutydate = $row['MyDate']; if($dutydate != $previousdate) { echo $dutydate.'<br />'; } elseif(!empty($dutydate)) { echo $dutydate.'<br />'; } echo $event1.' | '.$event2.' | '.$event3.'<br />'; $previousdate = $dutydate; } ?>
-
I seem to be struggling massively with this little problem. im sure its just a little something I am missing but it has be winding me up for hours. This is the said code. <? $id = $_POST['id']; $tags = $_POST['tags']; foreach( $id as $n ) { $tags = $tags[$n]; echo '<br><br>'.$tags.'---'.$n.'<br>'; $tags = explode(", ", $tags); $num=count($tags); echo "$num<br>"; foreach( $tags as $tag ) { print "ID: ".$n." --- tag: ".$tag." <br>\n"; } } ?> It finishes one full loop through fine, separates out all of the tags and puts them with the correct id. But when it moves onto the second full loop it seems to be getting nothing out of here $tags = $tags[$n]; I have tried changing the $n for one of the id numbers to make sure it is pulling the data from the $_POST[] and it is. The following is the results I am getting. cheese, more cheese, even more cheese---409 3 ID: 409 --- tag: cheese ID: 409 --- tag: more cheese ID: 409 --- tag: even more cheese ---287 1 ID: 287 --- tag: ---288 1 ID: 288 --- tag: ---406 1 ID: 406 --- tag: ---407 1 ID: 407 --- tag: ---408 1 ID: 408 --- tag: Any help or pointers in the right direction would be a massive help. Eric
-
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!
-
Hi I'm really hoping you can help me, and I'm trying to give as much information as I can, I'm getting an invalid argument supplied for foreach() error, which is referencing the following code (see below) - is their anything obvious you can see? I appreciate I'm a thicko <section class="categorynews"> <?php $categories = thb_HomePageCategories();?> <?php foreach($categories as $category) { ?> <?php $color = GetCategoryColor($category); ?> <div class="categoryholder cf"> <div class="categoryheadline" style="border-color:<?php echo $color; ?>"> <h2><?php $cat = get_category($category); echo $cat->name; ?></h2> <span><a href="<?php echo get_category_link($category); ?>" style="color:<?php echo $color; ?>"><?php _e( '<i class="icon-long-arrow-right"></i> View All Articles', THB_THEME_NAME ); ?></a></span> </div>
- 1 reply
-
- php
- invalid argument
-
(and 1 more)
Tagged with:
-
Dear all, I have a script on my website's front page. Quickly summarized: - I have two main categories ("Made by me" and "Made by others") - I have five subcategories I want to have on my frontpage two columns (one for main category #1, two for main category #2) containing each two postings. I use two loops: <!-- Start the Loop. --> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php static $count = 0; if ($count == "2") { break; } else { ?> <?php if ( in_category('5') && !is_single() ) continue; ?> <!--Start Post--> <div align="justify" style='float:left; width: 276px; margin: 0 0 0 20px;'> <h6><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><div style="color: #000000;"><?php the_title(); ?></div></a></h6> <div style="font-size: 80%; color: #999999; font-style: italic; font-weight: bold;">In: <?php //exclude these from displaying $exclude = array("4"); //set up an empty categorystring $catagorystring = ''; //loop through the categories for this post foreach((get_the_category()) as $category) { //if not in the exclude array if (!in_array($category->cat_ID, $exclude)) { //add category with link to categorystring $catagorystring .= ''.$category->name.', '; } } //strip off last comma (and space) and display echo substr($catagorystring, 0, strrpos($catagorystring, ',')); ?></div> <?php $my_excerpt = get_the_excerpt(); echo "<div style=\"color: #000000;\">"; echo $my_excerpt; echo "</div>"; ?> <?php wp_link_pages(array('before' => '' . __('Pages:', 'cloriato'), 'after' => '')); ?> <div class="frontpage_olav"> <a class="read_more" href="<?php the_permalink(); ?>"></a></div> </div> <!--End Post--> <?php $count++; } ?> <?php endwhile; ?> <!--End Loop--> <?php endif; ?> </div> And for the second column: <!-- Start the Loop. --> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php static $count2 = 0; if ($count2 == "2") { break; } else { ?> <?php if ( in_category('4') && !is_single() ) continue; ?> <!--Start Post--> <div align="justify" style='float:left; width: 276px; margin: 0 0 0 17px;'> <h6><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><div style="color: #000000;"><?php the_title(); ?></div></a></h6> <div style="font-size: 80%; color: #999999; font-style: italic; font-weight: bold;">In: <?php //exclude these from displaying $exclude2 = array("5"); //set up an empty categorystring $catagorystring2 = ''; //loop through the categories for this post foreach((get_the_category()) as $category2) { //if not in the exclude array if (!in_array($category2->cat_ID, $exclude2)) { //add category with link to categorystring $catagorystring2 .= ''.$category2->name.', '; } } //strip off last comma (and space) and display echo substr($catagorystring2, 0, strrpos($catagorystring2, ',')); ?></div> <?php $my_excerpt2 = get_the_excerpt(); echo "<div style=\"color: #000000;\">"; echo $my_excerpt2; echo "</div>"; ?> <?php wp_link_pages(array('before' => '' . __('Pages:', 'cloriato'), 'after' => '')); ?> <div class="frontpage_olav"> <a class="read_more" href="<?php the_permalink(); ?>"></a></div> </div> <!--End Post--> <?php $count2++; } ?> <?php endwhile; ?> <!--End Loop--> <?php endif; ?> The whole problem is now that often the first column only shows one posting instead of two. Sometimes, however, it does show two postings. What am I doing wrong? Why doesn't it show two postings as it's supposed to do? How come my first column doesn't function properly? Thanks a lot for all your help! Olav
-
I need to output in product-list.tpl file the result of a query from within classes/Search.php in Prestashop. The query in classes/Search.php is: $sql = 'SELECT * FROM `'._DB_PREFIX_.'category_group` cg INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category` INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category` INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product` INNER JOIN `'._DB_PREFIX_.'product_part_number` ON p. `id_product` = `ID` '.Shop::addSqlAssociation('product', 'p', false).' WHERE c.`active` = 1 AND product_shop.`active` = 1 AND product_shop.`visibility` IN ("both", "search") AND product_shop.indexed = 1 AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN ( SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.(int)$id_customer.' )'); $results = $db->executeS($sql); This is outputting a series of "GREKA" values in the `'._DB_PREFIX_.'product_part_number` table at the top of the search results page from the query above: $eligible_products = array(); foreach ($results as $row){ $eligible_products[] = $row['id_product']; echo $row['GREKA']; } What I need it to do is output the "GREKA" value within the tpl file themes/product-list.tpl for that product. This tpl file is using Smarty to output the results of the product search, I need to know how to call the "GREKA" value from the query to the foreach loop. I imagine it would be something like this: {foreach from=$products item=product name=products} {$GREKA} {/foreach} I have tried to put this line of code inside the foreach on the product-list.tpl and it gives me no output , it gives me an error: Code: {$product.GREKA} Error: Notice: Undefined index: GREKA
-
Hey guys, I'm having the hardest time trying to access data within my foreach loop. Here is the var_dump right before the foreach. array(19) { [0]=> string( "Thursday" [1]=> string( "Saturday" [2]=> string(6) "Monday" [3]=> string(6) "Friday" [4]=> string(6) "Monday" [5]=> string(9) "Wednesday" [6]=> string(7) "Tuesday" [7]=> string(7) "Tuesday" [8]=> string( "Thursday" [9]=> string(6) "Sunday" [10]=> string(7) "Tuesday" [11]=> string(7) "Tuesday" [12]=> string(6) "Monday" [13]=> string(6) "Friday" [14]=> string(9) "Wednesday" [15]=> string(6) "Monday" [16]=> string(9) "Wednesday" [17]=> string(6) "Sunday" [18]=> string(6) "Monday" } array(19) { [0]=> string(90) "http://www.mylink.com/1" [1]=> string(90) "http://www.mylink.com/2" [2]=> string(90) "http://www.mylink.com/3" [3]=> string(90) "http://www.mylink.com/4" [4]=> string(90) "http://www.mylink.com/5" [5]=> string(90) "http://www.mylink.com/6" [6]=> string(90) "http://www.mylink.com/7" [7]=> string(90) "http://www.mylink.com/8" [8]=> string(90) "http://www.mylink.com/9" [9]=> string(90) "http://www.mylink.com/10" [10]=> string(90) "http://www.mylink.com/11" [11]=> string(90) "http://www.mylink.com/12" [12]=> string(90) "http://www.mylink.com/13" [13]=> string(90) "http://www.mylink.com/14" [14]=> string(90) "http://www.mylink.com/15" [15]=> string(90) "http://www.mylink.com/16" [16]=> string(90) "http://www.mylink.com/17" [17]=> string(0) "" [18]=> string(0) "" } array(19) { [0]=> string(6) "715.00" [1]=> string(6) "912.00" [2]=> string(6) "715.00" [3]=> string(6) "715.00" [4]=> string(6) "715.00" [5]=> string(6) "715.00" [6]=> string(6) "715.00" [7]=> string(6) "912.00" [8]=> string(6) "715.00" [9]=> string(6) "715.00" [10]=> string(6) "715.00" [11]=> string(6) "715.00" [12]=> string(6) "715.00" [13]=> string(6) "912.00" [14]=> string(6) "912.00" [15]=> string(6) "715.00" [16]=> string(6) "715.00" [17]=> string(6) "715.00" [18]=> string(6) "912.00" } array(19) { [0]=> string(6) "330.00" [1]=> string(6) "400.00" [2]=> string(6) "330.00" [3]=> string(6) "330.00" [4]=> string(6) "330.00" [5]=> string(6) "330.00" [6]=> string(6) "330.00" [7]=> string(6) "400.00" [8]=> string(6) "330.00" [9]=> string(6) "330.00" [10]=> string(6) "330.00" [11]=> string(6) "330.00" [12]=> string(6) "330.00" [13]=> string(6) "400.00" [14]=> string(6) "400.00" [15]=> string(6) "330.00" [16]=> string(6) "330.00" [17]=> string(6) "330.00" [18]=> string(6) "400.00" } array(19) { [0]=> string(6) "220.00" [1]=> string(6) "275.00" [2]=> string(6) "220.00" [3]=> string(6) "220.00" [4]=> string(6) "220.00" [5]=> string(6) "220.00" [6]=> string(6) "220.00" [7]=> string(6) "275.00" [8]=> string(6) "220.00" [9]=> string(6) "220.00" [10]=> string(6) "220.00" [11]=> string(6) "220.00" [12]=> string(6) "220.00" [13]=> string(6) "275.00" [14]=> string(6) "275.00" [15]=> string(6) "220.00" [16]=> string(6) "220.00" [17]=> string(6) "220.00" [18]=> string(6) "275.00" } array(19) { [0]=> string(6) "187.00" [1]=> string(6) "225.00" [2]=> string(6) "187.00" [3]=> string(6) "187.00" [4]=> string(6) "187.00" [5]=> string(6) "187.00" [6]=> string(6) "187.00" [7]=> string(6) "225.00" [8]=> string(6) "187.00" [9]=> string(6) "187.00" [10]=> string(6) "187.00" [11]=> string(6) "187.00" [12]=> string(6) "187.00" [13]=> string(6) "225.00" [14]=> string(6) "225.00" [15]=> string(6) "187.00" [16]=> string(6) "187.00" [17]=> string(6) "187.00" [18]=> string(6) "225.00" } array(19) { [0]=> string(6) "157.00" [1]=> string(6) "192.00" [2]=> string(6) "157.00" [3]=> string(6) "157.00" [4]=> string(6) "157.00" [5]=> string(6) "157.00" [6]=> string(6) "157.00" [7]=> string(6) "192.00" [8]=> string(6) "157.00" [9]=> string(6) "157.00" [10]=> string(6) "157.00" [11]=> string(6) "157.00" [12]=> string(6) "157.00" [13]=> string(6) "192.00" [14]=> string(6) "192.00" [15]=> string(6) "157.00" [16]=> string(6) "157.00" [17]=> string(6) "157.00" [18]=> string(6) "192.00" } array(19) { [0]=> string(6) "138.00" [1]=> string(6) "165.00" [2]=> string(6) "138.00" [3]=> string(6) "138.00" [4]=> string(6) "138.00" [5]=> string(6) "138.00" [6]=> string(6) "138.00" [7]=> string(6) "165.00" [8]=> string(6) "138.00" [9]=> string(6) "138.00" [10]=> string(6) "138.00" [11]=> string(6) "138.00" [12]=> string(6) "138.00" [13]=> string(6) "165.00" [14]=> string(6) "165.00" [15]=> string(6) "138.00" [16]=> string(6) "138.00" [17]=> string(6) "138.00" [18]=> string(6) "165.00" } array(19) { [0]=> string(5) "89.00" [1]=> string(6) "125.00" [2]=> string(5) "75.00" [3]=> string(5) "95.00" [4]=> string(5) "86.00" [5]=> string(5) "86.00" [6]=> string(5) "75.00" [7]=> string(6) "150.00" [8]=> string(5) "79.00" [9]=> string(5) "77.00" [10]=> string(6) "103.00" [11]=> string(6) "103.00" [12]=> string(5) "75.00" [13]=> string(6) "118.00" [14]=> string(6) "125.00" [15]=> string(5) "89.00" [16]=> string(5) "75.00" [17]=> string(5) "86.00" [18]=> string(6) "135.00" } array(19) { [0]=> string(5) "97.00" [1]=> string(6) "115.00" [2]=> string(5) "97.00" [3]=> string(5) "97.00" [4]=> string(5) "97.00" [5]=> string(5) "97.00" [6]=> string(5) "97.00" [7]=> string(6) "115.00" [8]=> string(5) "97.00" [9]=> string(5) "97.00" [10]=> string(5) "97.00" [11]=> string(5) "97.00" [12]=> string(5) "97.00" [13]=> string(6) "115.00" [14]=> string(6) "115.00" [15]=> string(5) "97.00" [16]=> string(5) "97.00" [17]=> string(5) "97.00" [18]=> string(6) "115.00" } array(19) { [0]=> string(5) "81.00" [1]=> string(6) "105.00" [2]=> string(5) "60.00" [3]=> string(5) "81.00" [4]=> string(5) "71.00" [5]=> string(5) "71.00" [6]=> string(5) "61.00" [7]=> string(6) "125.00" [8]=> string(5) "69.00" [9]=> string(5) "69.00" [10]=> string(5) "81.00" [11]=> string(5) "81.00" [12]=> string(5) "60.00" [13]=> string(5) "96.00" [14]=> string(6) "105.00" [15]=> string(5) "75.00" [16]=> string(5) "60.00" [17]=> string(5) "71.00" [18]=> string(6) "115.00" } array(19) { [0]=> string(5) "78.00" [1]=> string(5) "90.00" [2]=> string(5) "78.00" [3]=> string(5) "78.00" [4]=> string(5) "78.00" [5]=> string(5) "78.00" [6]=> string(5) "78.00" [7]=> string(5) "90.00" [8]=> string(5) "78.00" [9]=> string(5) "78.00" [10]=> string(5) "78.00" [11]=> string(5) "78.00" [12]=> string(5) "78.00" [13]=> string(5) "90.00" [14]=> string(5) "90.00" [15]=> string(5) "78.00" [16]=> string(5) "78.00" [17]=> string(5) "78.00" [18]=> string(5) "90.00" } array(19) { [0]=> string(5) "50.00" [1]=> string(5) "60.00" [2]=> string(5) "50.00" [3]=> string(5) "50.00" [4]=> string(5) "50.00" [5]=> string(5) "50.00" [6]=> string(5) "50.00" [7]=> string(5) "60.00" [8]=> string(5) "50.00" [9]=> string(5) "50.00" [10]=> string(5) "50.00" [11]=> string(5) "50.00" [12]=> string(5) "50.00" [13]=> string(5) "60.00" [14]=> string(5) "60.00" [15]=> string(5) "50.00" [16]=> string(5) "50.00" [17]=> string(5) "50.00" [18]=> string(5) "60.00" } array(19) { [0]=> string(5) "28.00" [1]=> string(5) "35.00" [2]=> string(5) "28.00" [3]=> string(5) "28.00" [4]=> string(5) "28.00" [5]=> string(5) "28.00" [6]=> string(5) "28.00" [7]=> string(5) "35.00" [8]=> string(5) "28.00" [9]=> string(5) "28.00" [10]=> string(5) "28.00" [11]=> string(5) "28.00" [12]=> string(5) "28.00" [13]=> string(5) "35.00" [14]=> string(5) "35.00" [15]=> string(5) "28.00" [16]=> string(5) "28.00" [17]=> string(5) "28.00" [18]=> string(5) "35.00" } array(19) { [0]=> string(5) "13.00" [1]=> string(5) "25.00" [2]=> string(5) "13.00" [3]=> string(5) "13.00" [4]=> string(5) "13.00" [5]=> string(5) "13.00" [6]=> string(5) "13.00" [7]=> string(5) "25.00" [8]=> string(5) "13.00" [9]=> string(5) "13.00" [10]=> string(5) "13.00" [11]=> string(5) "13.00" [12]=> string(5) "13.00" [13]=> string(5) "25.00" [14]=> string(5) "25.00" [15]=> string(5) "13.00" [16]=> string(5) "13.00" [17]=> string(5) "13.00" [18]=> string(5) "25.00" } Here is the foreach: foreach ($values as $cell) { echo "<a href=\"".$link."\" class=\"".$class."\">" . $cell . "</a>"; } $class is supposed to be the first iteration of the array: Thursday, Saturday, Monday, etc. $link is supposed the be the second iteration of the array: http://www.mylink.com/... I can get the $cell data, but I cannot access the $link or $class values. Thank you so much for any help you can provide. ~SarahB
-
I've been away from PHP for awhile and need some help with a looping problem. In my example code below, I have two Mysql tables: tblDepRatesCats: ID | header | left_text | center_text | right_text | header_order tblRates_balance: id | depratecat | MinBalance | InterestRate | APY | suborder tblDepRatesCats.ID = tblRatesBalance.depratecat. For each row in tblDepRatesCats, there may be 0 or 1 or more rows in tblRates_balance. When results displayed, it should show each tblDepRatesCats row data followed by the related tblRatesBalance data (and the ability to add/edit more rates). My problem is that I can only get the first row of tblDepRatesCats and its relative tblRates_balance rows to appear. I can't figure out why it won't loop and show the other. You can view my test page here: http://www.bentleg.com/vieweditratecats7.php. My relevant test code is below. Thanks for any assistance. <form action="<? echo htmlentities($_SERVER['PHP_SELF']); ?>" name="Edit" method="POST"> <?php $sql = "select * from tblDepRatesCats order by header_order"; $result = $link->query($sql); while($row = mysqli_fetch_assoc($result)){ $ID[] = $row["ID"]; // for testing if(is_array($cattedid)){ echo "IS ARRAY"; }else{ echo"not array"; } echo implode(',',$ID); // end arrray test echo("<hr><input type=\"text\" name=\"ID[]\" value=\"" . $row["ID"] . "\"><p><tr><td>Category name/header text: <input type=\"text\" name=\"header$ID\" value=\"" . $row["header"] . " \" size=\"40\"> Order: <input type=\"text\" name=\"header_order$ID\" value=\"" . $row["header_order"] . "\" size=\"2\"><br> Left sub-text:<input type=\"text\" name=\"left_text$ID\" value=\"" . $row["left_text"] . "\" size=\"25\"> Center sub-text:<input type=\"text\" name=\"center_text$ID\" value=\"" . $row["center_text"] . "\" size=\"25\"> Right sub-text<input type=\"text\" name=\"right_text$ID\" value=\"" . $row["right_text"] . "\" size=\"25\"> <input type=\"checkbox\" name=\"delete_ids[]\" value=\"" . $row["ID"] . "\"> Mark to delete </p> <input type=\"submit\" name=\"editcat\" value=\"EDIT\"> </form> "); foreach ($ID as $new_depratecat) { $sql2="SELECT * FROM tblRates_balance where depratecat='$new_depratecat' ORDER BY suborder"; $result = $link->query($sql2); ?> <div style="width:90%;margin:auto;"> <form method="POST" id="new_depratecat"> <div id="itemRows"> Minimum Balance: <input type="text" name="add_MinBalance" size="30" /> Interest Rate: <input type="text" name="add_InterestRate" /> APY: <input type="text" name="add_APY" /> Order: <input type="text" name="add_suborder" size="2"/> << Add data and click on "Save Changes" to insert into db. <br> You can add a new row and make changes to existing rows all at one time and click on "Save Changes." New entry row will appear above after saving. <?php while($rates = mysqli_fetch_array($result)): ?> <p id="oldRow<?=$rates['id']?>"> Minimum Balance: <input type="text" name="MinBalance<?=$rates['id']?>" value="<?=$rates['MinBalance']?>" /> Interest Rate: <input type="text" name="InterestRate<?=$rates['id']?>" value="<?=$rates['InterestRate']?>" /> APY: <input type="text" name="APY<?=$rates['id']?>" value="<?=$rates['APY']?>" /> Order: <input type="text" name="suborder<?=$rates['id']?>" value="<?=$rates['suborder']?>" /> <input type="checkbox" name="delete_ids[]" value="<?=$rates['id']?>"> Mark to delete</p> <?php endwhile;?> </div> <p><input type="submit" name="ok" value="Save Changes"></p> </form>
-
I have problem with my code. What i'm trying to do is loop through a 2d array first looking at the level so i should have 8 <div class="group">. I then need to loop through the inner part of the array and i need to create groups of 4 so i should start with <div class="slide"> and close it after 4 items and if there are more repeat this process. The problem lies with this part. If i take it out it works but i need to get this in like shown below in my example output Problem code <?php if ($count % NUMCOLS == 0) echo '<div class="slide">'; # new row ?> <?php echo $count; ?> <?php $count++; ?> <?php if ($count % NUMCOLS == 0) echo '</div>'; # new row ?> Full code <?php define ("NUMCOLS",4); ?> <?php foreach ($this->level as $level => $courses): ?> <?php $count = 0; ?> <div class="group" id="level<?=$level;?>"> <div class="controls"><button class=prev>Previous</button> <button class=next>Next</button></div> <div class="pics" data-fx="scrollLeft" data-speed=300> <?php foreach ($courses as $course => $records): ?> <?php foreach ($records as $record): ?> <?php if ($count % NUMCOLS == 0) echo '<div class="slide">'; # new row ?> <?php echo $count; ?> <?php $count++; ?> <?php if ($count % NUMCOLS == 0) echo '</div>'; # new row ?> <?php endforeach; ?> <?php endforeach; ?> </div> </div> <?php endforeach; ?> Actual output <div class="group" id="level1"> <div class="controls"><button class=prev>Previous</button> <button class=next>Next</button></div> <div class="pics" data-fx="scrollLeft" data-speed=300> <div class="group" id="level2"> <div class="controls"><button class=prev>Previous</button> <button class=next>Next</button></div> <div class="pics" data-fx="scrollLeft" data-speed=300> <div class="slide"> </div> <div class="slide"> </div> </div> </div> </div> </div> desired output <div class="group" id="level1"> <div class="controls"><button class=prev>Previous</button> <button class=next>Next</button></div> <div class="pics" data-fx="scrollLeft" data-speed=300> <div class="slide"> </div> <div class="slide"> </div> </div> </div> <div class="group" id="level2"> <div class="controls"><button class=prev>Previous</button> <button class=next>Next</button></div> <div class="pics" data-fx="scrollLeft" data-speed=300> <div class="slide"> </div> <div class="slide"> </div> </div> </div>
-
Hello, I have yet another problem I am trying to understand. The thing I don't get is the foreach loop. Here's my code. I have a function to get the posts from the db as follows: function get_posts($connection) { $posts = array(); $sql = "SELECT * FROM posts ORDER BY stamp DESC"; $result = mysqli_query($connection, $sql); while ($post = mysqli_fetch_object($result)) { $posts = array('body' => $post->body, 'stamp' => $post->stamp, 'post_id' => $post->id, 'user_id' => $post->user_id); } return $posts; } And I'm (incorrectly) using a foreach loop to display the results like this: $posts = get_posts($connection); foreach ($posts as $key => $value) { echo $posts['body'] . "<hr>"; } If I echo $posts['body'] I get the first record showing up 4 times. I've used var_dump($posts) to make sure it's an array, and it is, and it shows the same first record 4 times. Any idea? I'm currently re-reading http://www.php.net/manual/en/control-structures.foreach.php to try and understand this. Thanks, Andrei
-
Hey guys, I am working for the first time without a database and MySQL while using PHP. anyway i am working on a real estate page and i am having issues on the "areas we cover", and this section would allow a user to view the neighborhoods or locations that the real estate agent has listings or does showings. i created a multidimensional array with the data i need. <?php $cities = array('pasadena' => array( "name" => "Pasadena", "picture" => "pasadena.png", "link" => "https://maps.google.com/maps?q=pasadena+ca&ie=UTF-8&ei=21uRUqGUKY3VoATvsILwDA&ved=0CAgQ_AUoAg", "value" => "1", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Pasadena/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Pasadena/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'glendale' => array( "name" => "Glendale", "picture" => "glendale.png", "link" => "https://maps.google.com/maps?q=glendale+ca&ie=UTF-8&ei=wFuRUvSBFNLroASkvYKgDw&ved=0CAgQ_AUoAg", "value" => "2", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Glendale/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Glendale/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'eagle rock' => array( "name" => "Eagle Rock", "picture" => "eagle.png", "link" => "https://maps.google.com/maps?q=eagle+rock+maps&ie=UTF-8&ei=cluRUtLDB9LuoASw8YDoDw&ved=0CAgQ_AUoAg", "value" => "3", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Eagle_Rock/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Eagle_Rock/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'silverlake' => array( "name" => "Silverlake", "picture" => "silverlake.png", "link" => "https://maps.google.com/maps?q=Silverlake+Ca&ie=UTF-8&ei=VFuRUrOLJMXwoASt74CQDQ&ved=0CAoQ_AUoAg", "value" => "4", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Silver_Lake/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:absolute;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/90026"></script></div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'atwater' => array( "name" => "Atwater Village", "picture" => "atwater_village.png", "link" => "https://maps.google.com/maps?q=atwater+village&ie=UTF-8&hq=&hnear=0x80c2c0e875cbce3b:0x99ebb9fd977ae16f,Atwater+Village,+Los+Angeles,+CA&gl=us&ei=kVGRUrzgHoTloAT_u4HADg&ved=0CKYBELYD", "value" => "5", "details" => "This is where the details will go", 'data' => '', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:absolute;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/90026"></script></div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'palmdale' => array( "name" => "Palmdale", "picture" => "palmdale.png", "link" => "https://maps.google.com/maps?q=Palmdale+Ca&ie=UTF-8&ei=G1qRUti1NonuoASHsIK4Cw&ved=0CAoQ_AUoAg", "value" => "6", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Palmdale/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Palmdale/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'lancaster' => array( "name" => "Lancaster", "picture" => "lancaster.png", "link" => "https://maps.google.com/maps?q=Lancaster+CA&ie=UTF-8&ei=elqRUqT_D8HroASK-oGwDw&ved=0CAgQ_AUoAg", "value" => "7", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Lancaster/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Lancaster/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'burbank' => array( "name" => "Burbank", "picture" => "burbank.png", "link" => "https://maps.google.com/maps?q=Burbank+Ca&ie=UTF-8&ei=qlmRUuKXF5HroATysoJI&ved=0CAoQ_AUoAg", "value" => "8", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Burbank/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Burbank/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'altadena' => array( "name" => "Altadena", "picture" => "altadena.png", "link" => "https://maps.google.com/maps?q=altadena+ca&ie=UTF-8&ei=01iRUrW1I8zloATwn4KgBA&ved=0CAoQ_AUoAg", "value" => "9", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Altadena/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Altadena/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), 'z_all' => array( "name" => "Sorry! Didn't find your city? We can show more.", "picture" => "all.png", "link" => "contact_us.php", "value" => "10", "details" => "This is where the details will go", 'data' => '<div id="tGraphs" style="border: 1px solid #ccc; padding: 5px 0px 5px 0px; width:300px; text-align:center;"><script language="javascript" type="text/javascript" src="http://synd.trulia.com/tools/chart/style_default300/type_average_listing_price/CA/Altadena/"></script></div>', 'second_image' => '<img src="altadena_home.jpg" height="195" width="200">', 'trulia' => '<style type="text/css"> .module {overflow:visible;}/*For typepad*/ #tMinistats { position:relative; width:180px; } #tMinistats .tMinistatsS { text-align:center; position:relative;bottom:0;margin-bottom:70px;width:95%; z-index:100;color:#b6d46f;font-family:Arial;font-size:11px; } #tMinistats .tMinistatsS a { color:#06c; }</style><div id="tMinistats" style="background-color:#ffffff;"> <script type="text/javascript" language="javascript" src="http://synd.trulia.com/tools/ministats/style_default180/CA/Altadena/"></script> </div>', 'second_image' => '<div style="position:relative; top:-30px; left:10px; border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena1.jpg" alt="Altadena Home" height="250" width="220"></div>', 'third_image' => '<div style="position:relative; top:-282px; left:260px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena2.jpg" alt="Altadena Home" height="250" width="220"></div>', 'fourth_image' => '<div style="position:relative; top:-535px; left:510px;margin-bottom: -500px;border: 2px solid black; height: 250px; width: 220px;"><img src="images/cities/altadena/altadena3.jpg" alt="Altadena Home" height="250" width="220"></div>' ), ); sort($cities); ?> I originally created this page as a single page layout that would have all the areas listed then would have them looking like a nice organized list. i called this page area_showings.php i created a foreach() loop that would call the array then the value foreach($cities as $city => $data){} this worked perfectly and was calling the data to the variable value and i could set the page up. I then decided that the idea of a long list of cities was not very user friendly and i decided to make is a dynamic page with a different structure. i wanted the user to go on area_showings.php and there he would select a link of his city interested in, and the page would then load the data based upon what the user had selected. <?php foreach ($cities as $city => $data){ echo <a href="area_showings.php?city=' . $data['name'] .'">'. $data['name'] . '</br></div>'; } ?> Now when a user selects the city he should be directed to the same page but now it would show the direct data based on the URL. This works fine and is passed correctly with the $_GET. MY ISSUE: I have created my conditional to check if the $_GET is empty or matches the correct city. but after the conditional i cant seem to understand how i can call the same $data[]; without a loop. i am use to MySQL where you can call for the $data using fetch_assoc to create the variables. if (empty($_GET['city']) === true){ ?> <div style="background-color:#A9B9C9;width:780px; height: auto; padding-left: 30px;padding-top: 30px; margin-left: 200px; margin-bottom: 20px; margin-top: 10px; border: black 5px solid;line-height: 25px;"> <?php foreach ($cities as $city => $data){ echo '<div style="height: 50px; width: auto; border: black solid 2px;font-size: 1.2em; padding: 10px 10px;"> <a href="area_showings.php?city=' . $data['name'] .'">'. $data['name'] . '</br></div>'; } ?> </div> <?php } else if (empty($_GET['city']) === false) { $name = $_GET['city']; if($name == 'Burbank' || $name == 'Glendale' || $name == 'Atwater Village' || $name == 'Pasadena' || $name == 'Altadena' || $name == 'Eagle Rock' || $name == 'Silverlake' || $name == 'Lancaster' || $name == 'Palmdale' || $name == 'Echo Park' || $name == 'Sorry! Didn\'t find your city? We can show more.'){ echo '<div style="background-color: #fff;padding: 10px 10px;height: auto; width:750px;border: 2px solid #000; border-radius: 15px;"> echo '<div style="background-color: #fff;padding: 10px 10px;height: auto; width:750px;border: 2px solid #000; border-radius: 15px;"> <div style="font-family:Lato, sans-serif, Helvetica, sans-serif;color:#2B4450; font-size: 30px; font=weight:bolder;margin-bottom:15px;margin-top:15px; text-transform: uppercase;">' . $data['name'] . '</div> <div style="height:250px; width:200px; border: black 4px solid; margin-bottom: -200px;"><a href="' . $data['link'] .'" target="_blank"><img src="images/cities/' . $data['picture'] . '" height="250" width="200"></a></div> <div style="position:relative;top:-55px;left:220px;height:250px; width: 0px;line-height:-50px;margin-top:-120px; padding-left: 10px; padding-top: 10px;border-radius: 10px;">' .$data['data'] . '<span style="position:relative; top: -200px; left: 320px;">' .$data['trulia'] . '</span></div><div style="width:auto; height; 300px; border-bottom: 3px solid black;">' . $data['second_image'] . $data['third_image'] . $data['fourth_image'] . '</div> <h2><span style="color:#2B4450; font-weight: bolder; font-family:Lato, sans-serif, Helvetica, sans-serif;">For more info</span> <a href="contact_us.php" class="info_Button">Click Here</a></h2> </div> </br>'; </div> </br>'; } } so just to clarify, i want to be able to use the correct array of data to the matching city of the $_GET with a multidimensional array without using a foreach loop. i want the info to be dynamic not listed. any suggestions ? implode function ? array_walk ?
- 3 replies
-
- multidimensional
- array
-
(and 3 more)
Tagged with:
-
Hello all. I have tried to figure out why I can't make a certain part of my code sticky. I am attempting to save the user's input for the radio button section. I am using a foreach loop for everything in my array. When I try to use if isset, it returns the last item in the array instead of what was selected by the user. Here is the code: <?php //Creating foreach loop for magazine selection foreach ($magSubs as $mag => $SUB_PRICE) { echo "<input type='radio' name='magtype' value='$mag' />$mag $$SUB_PRICE per month<br> "; } ?> Again, I want to make this part sticky.
-
Hello guys, I'm having trouble making a loop to display the data in a table, I need the loop to display a table with the following data: example: 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 My code: <?php echo '<table width="100%" border="1" cellspacing="0" cellpadding="0">'; $contador = 1; while ($contador <= 10) { if($contador % 2){ for($x=0;$x<2;$x++){ echo "<tr><td>Registro $contador</td>"; } }else{ echo "<td>Registro $contador</td></tr>"; } ++$contador; } echo '</table>'; ?> Help me!
-
I am pulling some data out of a MySQL database and echoing it out (pretty standard stuff) What is really confusing me is I have checked the query using phpMyAdmin and it returns 1 row which is what i expect. I've even double checked by using a COUNT query and i get the same result. The problem is when I run the returned array through a foreach loop to populate a new array, it appears to create 2 new array rows and i think it's to do with my IF statements. What i want to do in the IF is check is a certain value is present. If not, check that the value is a number and a certain length out of 2 conditions. If not, then do nothing. I'm looking for 3 conditions. Could the problem be that the field type in the database is a varchar and i'm checking for a number? Here is my code $allowed = array('param1', 'param2', 'param3'); //if(isset($_POST['submit'])) { //$sql = 'SELECT consignment_number FROM tracker WHERE order_number = 2893214 AND consignment_number IS NOT NULL'; $sql = 'SELECT column1, column2 FROM table WHERE column1 = :placeholder AND CHAR_LENGTH(column2) > 1'; $stmt = $db->prepare($sql); //$stmt->bindValue('order', $_POST['order']); $stmt->bindValue(':placeholder', $val); $stmt->execute(); $rows = $stmt->fetchAll(); echo count($rows).'<br />'; //echos 1 as checked in phpMyAdmin if(count($rows) != 0) { $myarray = array(); foreach($rows as $row) { $test = $row['column2']; //do something if ($test == 'RM') { // despatched by royal mail $myarray['column1'] = "param1"; $myarray['column2'] = $test; } elseif ( ctype_digit($test) && (strlen($test) == ) { // going by FedEx? $myarray['column1'] = "param2"; $myarray['column2'] = $test; } elseif ( ctype_digit($test) && (strlen($test) == 11) ) { // going by TNT? $myarray['column1'] = "param3"; $myarray['column2'] = $test; } } } else { //no rows } echo count($myarray); echo '<pre>';var_dump($myarray);echo'</pre>'; if(in_array($myarray['column1'], $allowed)) { foreach ($myarray as $array) { if(!empty($myarray['column1'])) { echo $myarray['column1'].': '.$myarray['column2'].'<br />'; } } } else { }
-
I have got an array from a database and I want to change some of the values like using ucwords() etc. The problem is when i run the array through a foreach loop, it overwrites all the values in the array with the values of the last row in the array. I've been racking my brains all afternoon and can't figure out what's going wrong. foreach($rows as $key => $row) { //$rows['datetime'] = Forum::change_date(strtotime($row['postdate'])); $rows[$key]['board'] = strtolower(str_replace(array(' ','\''), array('-',''),$row['boardname'])); $rows[$key]['boardid'] = $row['boardno']; $rows[$key]['boardname'] = ucwords($row['boardname']); $rows[$key]['topicname'] = Words::shortenText($row['topicname'],30); } I end up with the 6 rows in the array all the same. Any ideas what I'm doing wrong?
-
tHis loop displays slider image as 1,1 2,2, 3,3 4,4 5,5 Instead of 1,2,3,4,5 can anyone help me <?php $results2=$objMain->getResults("SELECT * FROM tbl_imageslider"); $products3=$objMain->getResults("SELECT * FROM tbl_products"); ?> <div class="camera_wrap camera_ash_skin" id="camera_wrap_1"> <?php if(!empty($results2)){foreach($results2 as $res2){?> <?php if(!empty($products3)){ foreach($products3 as $pro3){ ?> <div data-thumb="images/post-images/banner-thumb.jpg" data-src="<?php echo $res2['image_path']; ?>" data-alignment="topRight"> <div class="camera_caption fadeFromBottom"> <div class="slideshow-content"> <h2><?php echo $pro3['page_title']; ?><span> </span> </h2> <div class="slideshow-desc"> <p><?php echo substr($pro3['page_content'],0,125); ?></p> <a href="<?php echo $pro3['page_url']; ?>" title="" class="slideshow-btn"> Read More </a> </div> </div> </div> </div> <?php }} ?> <?php }} ?> </div>
-
I have a multidimensional array like so. Array ( [0] => Array ( [0] => Yolo County Sheriff's Home 2008faroo [1] => http://www.yolosheriffs.com/ [2] => 86 ) [1] => Array ( [0] => Fremont, Yolo County, California - Wikipedia, the free encyclopediafaroo [1] => http://en.wikipedia.org/wiki/Fremont,_Yo… [2] => 11 ) [2] => Array ( [0] => The Lonely Island - YOLO (feat. Adam Levine & Kendrick Lamar) - YouTubefaroo [1] => http://www.youtube.com/watch?feature=pla… [2] => 45 ) What i need to do is find duplicate urls in this array, merge them together and combine the score found at [2]. I have scoured the php manual but I can't seem to find a way to merge the duplicate urls and then add the scores to the remaining url. Would it make it any easier to have it in the format Array ( [0] => Array ( [0] => Yolo County Sheriff's Home 2008faroo, array([0] => http://www.yolosheriffs.com/ ,[1] => 0) ) ?? All suggestions appreciated.I have looked at the manual but i don't have the knowledge at this stage to use the array functions in conjunction with foreach loops to achieve what i need.I
- 2 replies
-
- php
- multidimensional arrays
-
(and 3 more)
Tagged with:
-
I have this array: <?php $continents=array("Asia"=>array("Bangladesh","India","Pakistan"), "Europe"=>array("England","France"), "Africa"=>array("Kenya","Libya","Somalia")); ?> Now I would like to echo: In Africa there are: Kenya, Libya, Somalia. How can I do that? Can anyone please help me? Thanks in advance.
-
I have a select that allows a user to select an album. I then run a query to check the post of the select to make sure it's a real album number. So what I do is run the query then use in_array to see if the entered number is a good album number My problem is the first one doesn't return an array and when I use print_r I only get one value I can't get this one to work, why won't the in_array work? I thought $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC) would give me an array // Check for an album id if (empty($_POST['aid'])) { $errors [] = '<h5 style="color: red;">* You forgot to select an album.</h5>'; } elseif (is_numeric($_POST['aid'])) { $aid_entry = (int)(trim($_POST['aid'])); $aq ="SELECT aid FROM cpg15x_albums"; $ar = mysqli_query($dbc3, $aq);// or die("Error: ".mysqli_error($dbc3)); $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC); if (in_array($aid_entry, $aid_rows)) { $aid = $aid_entry; } else {$errors[] = '<h5 style="color: red;">* Album number error.</h5>';} } else {$errors[] = '<h5 style="color: red;">* Album string error.</h5>';} My new one that works but I have to use a while loop // Check for an album id if (empty($_POST['aid'])) { $errors [] = '<h5 style="color: red;">* You forgot to select an album.</h5>'; } elseif (is_numeric($_POST['aid'])) { $aid_entry = (int)(trim($_POST['aid'])); $aq ="SELECT aid FROM cpg15x_albums"; $ar = mysqli_query($dbc3, $aq);// or die("Error: ".mysqli_error($dbc3)); $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC); $aids = array(); while ($aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC)) { $aids[] = $aid_rows['aid']; } if (in_array($aid_entry, $aids)) { $aid = $aid_entry; } else {$errors[] = '<h5 style="color: red;">* Album number error.</h5>';} } else {$errors[] = '<h5 style="color: red;">* Album string error.</h5>';}
-
Hi, It's my first time to used foreach loop to display data from database and also used foreach loop to display textboxes Now, I felt difficulties in saving data from the textboxes displayed by foreach. Here is my code: <html> <head> </head> <body> <form name="operator_report" action="" method="post" autocomplete="off"> <?php ob_start(); include "connection.php"; $save = isset($_POST['save']); //insert data to database if($save) { $compound_type = $_POST['compound_type']; $op_output = $_POST['output']; $c_output = $_POST['output']; $reject_type[] = $_POST['reject_type']; $reject[] = $_POST['reject']; for($s = 0; $s < count($compound_type) ; $s++) { //---save operators output to op_output, in this part i don't have problem in saving the data saved correctly if ($op_output[$s] !="") { $sql = "INSERT INTO op_output (op_number, process_id, shift_id, shift_date, shift_type, compound_type, compound_output) VALUES ('001', '4', '1','2013-07-16', '1', '$compound_type[$s]', '$op_output[$s]')"; $result = mysql_query($sql, $con); } //---this part I need help in saving reject. //---I guess i have the problem here in looping ... //---this code did not work.. for($i = 0; $i < count($reject_type[$s]) ; $i++) { if($reject[$i] != "") { $sql_re = "INSERT INTO op_reject (op_number, compound_type, reject_type, reject) VALUES ('001','$compound_type[$s]', '$reject_type[$i]', '$reject[$i]')"; $result_re = mysql_query($sql_re, $con); } } } } $id = 'Final Mix'; if($id == 'Final Mix') { echo "<fieldset>"; echo "<legend><H1> Operator's Output and Reject</H1></legend>"; echo "<table>"; echo "<tr>"; echo "<th>Compound</th>"; echo "<th>Output</th>"; //query to select reject acronym $sql = "select r.reject_acro, r.reject_id from process_list AS p LEFT JOIN reject_list AS r ON p.reject_id = r.reject_process_id where p.process_name LIKE '" .$id. "%'"; $rsd = mysql_query($sql); while($rs = mysql_fetch_assoc($rsd)) { $reject[] = $rs['reject_acro']; $reject_id[] = $rs['reject_id']; } if(empty($reject)) { echo""; } else { //display reject acronym as header foreach ($reject as $reject) { echo "<th style='border:none;'><input type='text' name='reject_type[]' id='reject_type' value='$reject' style='border:none; font-weight: bold; ' size='5'></th>"; } echo "</tr>"; } //query select compound_type $sql_comp = "SELECT compound_id, compound_type FROM compound_list ORDER BY compound_type ASC"; $res = mysql_query($sql_comp); echo "<tr>"; while($comp = mysql_fetch_assoc($res)){ $compound_type = $comp['compound_type']; //---display compound_type echo "<td style='border:none;'><input type='text' name='compound_type[]' id='compound_type' value='$compound_type' style='border:none;' size='10'readonly='readonly'></td>"; //---input box for output echo "<td style='border:none;'><input type='text' name='output[]' id='output' value='' size='7'></td>"; //----i used foreach to loop textbox in every reject acronym. foreach($reject_id AS $reject) { echo "<td style='border:none;'><input type='text' name='reject[]' id='reject' value='' size='7'></td>"; } echo "</tr>"; } echo "</table>"; echo "</fieldset>"; } ?> <input type="submit" name="save" id="save" value="Save"> </form> </body> </html> I also attached my sample output screenshots and the database. Here is the sample scenario. Compound Type---Output---SC---SP---SH--- P28--------------10-------------1-----2 P32--------------20--------5--------------- P32NW--------------------------3---------- I need to save data on op_reject like this: op_number---compound_type----reject_type---reject 001----------P28---------------SP------------1 001----------P28---------------SH------------2 001----------P32---------------SC------------5 001----------P32NW------------SP------------3 Sad to say in my code in saving using forloop it did not work. I post it because i am hoping somebody can help me, but I also tried my best to fixed this. Any help is highly appreciated. Thank you so much. sample.zip
-
Hi guys i just want to be able to add this form to a mysql update command E.g. mysql_query("UPDATE table SET product_quantity = '.$product_count.' WHERE product_id = '.$product_id.' "); for each array. I done some test below just to see how it is processed... i need it to add like... Q then P then Q then P. Im not sure how to do it, i tried adding two foreach commands inside each other although that came up with 3 times the results. I was trying to add two foreach in one statement so i could reference to both at the same time. All help would be great. Thanks! Q:13 Q:1 Q:1 Q:12 P:3 P:5 P:1 P:4 include_once('content/page_header.php'); //POST VARS $quantity_post = $_POST['quantity']; $product_post = $_POST['product_id']; $string = ""; foreach($quantity_post as $quantity){ $string .= "Q:".$quantity."<br/>"; } foreach($product_post as $product){ $string .= "P:".$product."<br/>"; } echo $string;
-
I'm trying to create a list of Rosters all on one page from a data table. I want each roster to show its Team Number ($team) and coaches name ($nameCoach) at the top of the list. I tried moving the Foreach loop around, but I can't get the results I want. So it should look like: A1 - Coach Brett Crist player name player name player name etc A2 - Coach Paul Goser player name player name etc Here is the link to the page. Basically, for each player it lists all the coaches. http://metroindybasketball.com/resources/league2012/2012rosters.php $coach = array ( 'A1' =>'Coach Brett Crist', 'A2' =>'Coach Paul Gosser', 'A3' =>'Coach Brian Tonsani', 'B1' =>'Coach Dustin Harvey', 'B2' =>'Coach Kyle Smith', 'B3' =>'Coach Trevor Andershock', 'C1' =>'Coach Jim Reamer', 'C2' =>'Coach Aaron Butcher', 'C3' =>'Coach Bob Cerbone', 'D1' =>'Coach Larry Baker', 'D2' =>'Coach John Tate', 'D3' =>'Coach Victor Stallworth', 'E1' =>'Coach Mark Bailey', 'E2' =>'Coach Angelo Smith', 'E3' =>'Coach Mike Lawson', 'E4' =>'Coach Jeremy Tiers', 'F1' =>'Coach Aaron Butcher', 'F2' =>'Coach Duke Pryor', 'F3' =>'Coach Damon Hawkins', 'F4' =>'Coach Roy Hairston', 'G1' =>'Coach Kristof Kendrick', 'G2' =>'Coach Michael Tucker', 'G3' =>'Coach Kenton Granger', 'G4' =>'Coach Steve Turner', 'H1' =>'Coach Seve Beach', 'H2' =>'Coach Matt Lacey', 'H3' =>'Coach Derrick Gentry', 'H4' =>'Coach Kayle Funkhouser', 'I1' =>'Coach Courtney James', 'I2' =>'Coach Barry Kroot', 'I3' =>'Coach Chris Hawkins', 'J1' =>'Coach Chris Sibila', 'J2' =>'Coach Larry Baker', 'J3' =>'Coach Dan Schukraft' ); $query = 'SELECT * FROM fallLeague10 WHERE 2012team IS NOT NULL ORDER BY 2012team,2012number'; $results = mysql_query($query) //or trigger_error('MySQL error: ' . mysql_error()) ; $currentTeam = false; while($line = mysql_fetch_assoc($results)) { if($currentTeam != $coach) { foreach ($coach as $team=>$nameCoach) { //Status has changed, display status header $currentTeam = $line['2012team']; echo '<tr><td colspan="6"><hr></td></tr> <tr><td colspan="6"><span class="coach">'. $team . ' - ' .$nameCoach. '</td></tr> '; } } echo '<tr> <td>' . $line['2012number'].'</td> <td><b>' . $line['nameFirst'] . ' ' . $line['nameLast'] . '</b></td> <td><center>'. $line['feet'] . '\'' . $line['inches'] . '"</center></td> <td><center>'. $line['grade'] . '</center></td> <td>'. $line['school'] . '</td>'; echo '<td><b>'. $line['college'].'</b></td>'; echo '</tr>';} echo '</tbody></table>';
-
Hello: I m having this foreach duplicated in returned data this happened when I upgraded to a news version of xammp and can t figure out why the duplicated are taking place... <?php if(isset($sess_uLOG) && $sess_uLOG==true) { $attrib = array('name'=>'fxxx','class' => 'fxxx'); echo form_open('main/profile',$attrib); ?> <ul class="cont_up"><!-- START section Overview--> <?php foreach ($u_profile as $u_pr) : ?> <li> <div class="sec_topic">Overview<span><?php $ubtn=array('name'=>'form_btn','value'=>'btn_save0','type'=>'submit','content'=>'Save');echo form_button($ubtn)?></span></div> <div class="profile_pic"> <img src="<?php echo base_url()."assests/imgs/profile/thumbs/".$u_pr->prof_pic; ?>" width="200" height="200" alt="<?php echo $u_pr->un; ?>"> </div> <div class="sec_content0"> <blockquote> <span>gender:</span><span> <SELECT name="ugender"> <?php @$u_gender=$this->input->post('ugender', TRUE); foreach ($ar_gender as $val_gender) { if ($u_gender != NULL && !empty($u_gender)) { echo "<option value='$val_gender'"; if ($val_gender == $u_gender) {echo " selected='selected'";} echo ">".$val_gender."</option>\n"; } else { echo "<option value='$val_gender'"; if ($val_gender == $u_pr->gender) {echo " selected='selected'";} echo ">".$val_gender."</option>\n"; }//END if else post } ?> </SELECT> <span>name:</span> <span> <input type="text" name="ufname" value="<?php echo isset($_POST['ufname']) ? $_POST['ufname'] : $u_pr->ufn; ?>" maxlength="12"> <input type="text" name="ulname" value="<?php echo isset($_POST['ulname']) ? $_POST['ulname'] : $u_pr->uln; ?>" maxlength="12"> </span> <span>username:</span> <span><input type="text" name="uname" value="<?php echo isset($_POST['uname']) ? $_POST['uname'] : $u_pr->un; ?>" maxlength="18"></span> <span>country:</span><span> <SELECT name="ucountry"> <?php @$u_count=$this->input->post('ucountry'); foreach ($country as $value) { if ($u_count != NULL && !empty($u_count)) { echo "<option value='$value'"; if ($value == $u_count) {echo " selected='selected'";} echo ">".$value."</option>\n"; } else { echo "<option value='$value'"; if ($value == $u_pr->u_country) {echo " selected='selected'";} echo ">".$value."</option>\n"; }//END if else post } ?> </SELECT> </span> <span>phone: +</span><span><input type="text" name="uphone" value="<?php echo isset($_POST['uphone']) ? $_POST['uphone'] : $u_pr->u_phone; ?>" maxlength="18"></span> </blockquote> </div> </li> <?php endforeach; ?> </ul><!-- END section Overview--> <?php echo form_close(); }else{ .... }
- 5 replies
-
- php
- codeigniter
-
(and 2 more)
Tagged with:
-
if (is_array($_POST['uid'])) { foreach($_POST['hours']AS $keyd => $value) { echo "UPDATE participantlog SET noshow=1 AND hours=" . $value . " WHERE cid=" . $cid . " AND uid=" . $keyd . "<br>"; mysql_query("UPDATE participantlog SET noshow='1' AND hours='$value' WHERE cid='$cid' AND uid='$keyd'") or die(mysql_error()); } } For some reason my query isn't executing. The foreach is working properly and producing all the right variables (as verified by the echo). But it just isn't updating the DB. Why might this be? Thanks! Clinton