Jump to content

Search the Community

Showing results for tags 'array'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • 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


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Donation Link

  1. Hi all I have a function below that searches an array and assigns it to $totalmonthlycost. I need to know how I get the array key of the chosen array value ($m12 - $m60) How do I return the array key in the below code? $monthvalues = array($m12,$m18,$m24,$m30,$m36,$m42,$m48,$m54,$m60); function closest($monthvalues, $number){ #does the array already contain the number? if($i = array_search( $number, $monthvalues)) return $i; #add the number to the array $monthvalues[] = $number; #sort and refind the number sort($monthvalues); $i = array_search($number, $monthvalues); #check if there is a number above it if($i && isset($monthvalues[$i-1])) return $monthvalues[$i-1]; //alternatively you could return the number itself here, or below it depending on your requirements } $totalmonthlycost = closest($monthvalues, $idealcostpermonth); Thanks for your help. Pete
  2. This one is a doozy for me. My brain is fried after spending a couple days trying to figure this out. None of my formulas have seemed to work out enough. So I'm waiving my white flag and asking for some help. Below is a simplified version of my code. The purpose of this page is an estimate calculator. It is for a window cleaning service, and they have pricing for doing the insides of windows and the outsides. Each style of window has different pricing for each inside and outside. What is supposed to happen in the form is this: If they want an estimate for say basic window cleaning for both inside and outside, they will check the chechboxes for both inside and outside. Then enter a number for the amount of windows to clean. The formula for this would look something like this: Amount = Inside Price x Outside Price (if inside price and outside price checkboxes are ticked). And for each entry, it needs to add up the total. But none of the foreach or while loops i try to create seem to do the trick. Basic Form Setup: $query = "SELECT * FROM estimates WHERE 1 ORDER BY id ASC"; $result = mysql_query($query); while ($estimates = mysql_fetch_object($result)) { $style = $estimates->style; $insidePrice = $estimates->price; $outsidePrice = $estimates->out; echo $style.' <input type="checkbox" name="insidePrice[]" value ="'.$insidePrice,'" /> $'.$insidePrice.' <input type="checkbox" name="outsidePrice[]" value ="'.$outsidePrice,'" /> $'.$outsidePrice.' <input type="text" name="amount[]" />'; } And an idea of how i want this to work: foreach ($_POST['amount'] as $key => $a){ if ($a != "" or $a !="0"){ //Now to llustrate what Im trying to do, I know this is wrong though foreach ($_POST['insideAmount'] as $i){ $i = $i*$a; } foreach ($_POST['outsideAmount'] as $o){ $o = $o*$a; } $a = $i+$o; } //Trying to add each row as they build to make the estimate total $prev = $a[$key-1]; $a = $a+$prev; $total = $a; } echo "$total";
  3. The website I am working on is a HRIS project for a popular fast food franchise chain. The login for one restaurant allows one user to modify all actions for all restaurants owned by the same franchisee. These are organized by multiple databases calling the same id's. Below is the code used for selecting all restaurants where the same franchise id is used as the restaurant login. $result = mysql_query("SELECT id, name FROM restaurants WHERE franchise = $_SESSION[sESS_RESTAURANT_FRANCHISE]"); $_SESSION['franchise_arr'] = array(); while($row = mysql_fetch_array($result)){ $_SESSION['franchise_arr'][$row['id']] = $row['name']; } So lets say for one login the SESSION array is populated like this: $_SESSION[sESS_RESTAURANT_FRANCHISE][1] = "Restaurant One"; $_SESSION[sESS_RESTAURANT_FRANCHISE][15] = "Restaurant Two"; $_SESSION[sESS_RESTAURANT_FRANCHISE][29] = "Restaurant Three"; I need a way to check to see if the KEY value is in an array as oppose to the value itself. For example I have: if (!in_array($rest, $_SESSION['franchise_arr'])){ $rest = $_SESSION['SESS_RESTAURANT_ID']; } However that checks for the values "Restaurant One", "Restaurant Two", "Restaurant Three". Where I would like it to look for "1", "15", or "29". I hope someone understands what I mean :\ I've been strugging with this for quite a while.
  4. Hi there mates, I've got a question from a friend of mine. And I could not solve it and I am ashamed of myself haha . The code is not yet complete, but what it has to do, is post the array, whick contains the names of the files uploaded ( pictures in this case ) The first post works, but thats it, it doesnt show the array on the last past it only shows "array". Please help me fix problem, I translated a bit of the code from dutch to english, so it's a bit more clear Thanks in advance David fotoinvoerpagina2.php
  5. Hi, I'm hoping this is something quite basic. I'm trying to group values from a MySQL query into an array. I have a Wordpress set-up but I'm presuming this is more of a PHP rather than a Wordpress concern. I just want to know basically what is the best way of doing this. Essentially I currently have the following data: Artist Name #1 - Event Date #1 Artist Name #2 - Event Date #2 Artist Name #1 - Event Date #3 Artist Name #1 - Event Date #4 And I want to find a way that I can group the Artist Names so that the data would output like this: Artist Name #1 Event Date #1 Event Date #3 Event Date #4 Artist Name #2 Event Date #2 This is the code I have at the moment. How would I integrate an array and an extra Foreach loop to get the data organised like this. I'm still learning Arrays at the moment and would appreciate any assistance with this. global $post; $args = array( 'post_type' => 'event', 'orderby' => 'meta_value', 'meta_key' => 'event_date', 'meta_compare' => '>', 'meta_value' => $todaysDate, 'numberposts' => 6, 'order' => 'ASC' ); $whatson = get_posts( $args ); foreach( $whatson as $post ) : echo get_the_title(get_post_meta($post->ID, '2artist', true)) . " - "; echo get_post_meta($whatson_query->ID, 'event_date', true); endforeach; Thanks! Russ
  6. HI guys, Everytime I run my php code I get this error message: This is my code: $query = mysql_query("SELECT * FROM Products WHERE cat='Skate' AND subcat='Footwear' LIMIT $start, $limit_img "); //end of query $limit_img = 24; $start = 0; $cols = 3; $cols_td = 0; //end of table variables $images = $row['photo']; $product = $row['product']; $price = $row['price']; //end of row variables echo "<table width='95%'><tr>"; //end of table while ($row = mysql_fetch_array($query)){ echo "<td><div align='center'>$row[product]</div></td>"; $cols_td = $col_td + 1; if($cols_td == $cols){ echo "</tr><tr>"; $cols_td = 0; } } //end of while statement echo "</tr></table>"; Thanks In advance!
  7. So i am trying to get a list of items out of my MySql database but my loop is not creating the array correctly, its dropping multiple rows when the key is the same name. <?php $search = 'item1'; $con = require_once('./dbconnect.php'); mysql_select_db("packages", $con); $package = mysql_query("SELECT * FROM $search", $con); while($item = mysql_fetch_row($package)){ $arr[$item[1]] = array('description' => $item[2], 'image' => $item[3]); } echo json_encode($arr); mysql_close($con); ?> So the loop works and does actually create the array with the various 'packages' in their respective array. The issue that i am having is that mysql_fetch_row($package)) can and will contain multiple keys that are the same name but with different things in the array. For example, the database will be something like this: PACK LEV DESCRIPTION IMAGES item1 A1 description for stuff1 image1 item1 A2 description for more stuff image2 item1 B1 more stuff here image3 item2 A1 description here for item2 image1 item2 B1 description contents here image2 --------------------------------------------------------------------------------- My Above code will produce an array like this: Array ( [item1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [description] => description contents here [image] => image2 ) ) What i want the array to be is: Array ( [item1] => Array ( [A1] => Array ( [description] => description for stuff1 [image] => image1 ) [A2] => Array ( [description] => description for more stuff [image] => image2 ) [b1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [A1] => Array ( [description] => description here for item2 [image] => image1 ) [b1] => Array ( [description] => description contents here [image] => image2 ) ) ) I hope this makes sense..Thanks for looking
  8. Hey guys, really hoping someone can help me with this I'll be extremely grateful! I am redesigning a website with a news archive type page using the mysql database from the previous version. The archive section contacts mysql and returns a number of values. In the archive section the previous version of the site uses a column called newsTitle to put information into the anchor url of the page which displays the full article. The problem is that there are duplicates in newsTitle. The previous website dealt with this by using php to add a suffix to the 1st duplicate of -0 , a suffix to the second duplicate of -1 and so on. I want to recreate this so I can keep the urls the same for seo purposes, otherwise I would just use the unique column newsID to seperate each article. Here's a simplistic version of my code so far. If anyone can come up with a way to add the suffixes to $title I will be soooo happy! Really appreciate your time and assistance. mysql_connect($host, $user, $pass) or die ("Unable to connect!"); @mysql_select_db($db) or die ("Unable to select database!"); $querytable="SELECT * FROM tblnews WHERE YEAR(newsDate) >= $startyear AND YEAR(newsDate) <= $endyear AND newsPageID = 13 ORDER BY `tblnews`.`newsDate` DESC"; $result=mysql_query($querytable); $sql = "SELECT * FROM tblnews WHERE YEAR(newsDate) >= $startyear AND YEAR(newsDate) <= $endyear AND newsPageID = 13 ORDER BY `tblnews`.`newsDate` DESC LIMIT $offset, $rowsperpage"; $result3 = mysql_query($sql, mysql_connect($host, $user, $pass)) or die ("Unable to connect!"); mysql_close(); $i= ($currentpage - 1) * 4; while ($list = mysql_fetch_assoc($result3)) { $title=mysql_result($result,$i,"newsTitle"); $url = strtolower ($title); ?> <div id="release"> <p> </p> <h3><a href="questionsarticle.php?pagetitle=<?php echo $url; ?>"><?php echo $title; ?></a></h3> <p> </p> </div> <?php $i++; }
  9. I have a multi-dimensional array (something as below). What am trying to do is find a specific value, and if that value has children, list only those children. So for example, if am searching for slug's "test", then all of "test's" children list on the page. "Test2" has no children, so none listed. If am searching for "/test/sub" (the slug), and since it has children, I wanted to show all of the children (eg. "sh", "rd", "co"). I have only a code for checking a multi-dimensional array, but am lost as to how to go beyond that. function in_array_r($needle, $haystack, $strict = true) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } Array ( [test] => Array ( [children] => Array ( [2] => Array ( [parent] => 4 [title] => Sub [slug] => test/sub/ [id] => 2 [children] => Array ( [3] => Array ( [parent] => 2 [title] => sh [slug] => test/sub/sh [id] => 3 ) [27] => Array ( [parent] => 2 [title] => RD [slug] => test/sub/rd [id] => 27 ) [145] => Array ( [parent] => 2 [title] => cO [slug] => test/sub/co [id] => 145 ) ) ) [8] => Array ( [parent] => 4 [title] => sub2 [slug] => test/sub2 [id] => 8 ) [19] => Array ( [parent] => 4 [title] => sub3 [slug] => test/sub3 [id] => 19 [children] => Array ( [20] => Array ( [parent] => 19 [title] => child1 [slug] => test/sub3/child1 [id] => 20 ) [21] => Array ( [parent] => 19 [title] => child2 [slug] => test/sub3/child2 [id] => 21 ) ) )
  10. Hi All, This code gives me the quantity output of the ordered products (per line): (int)($product['product_quantity']) Like: Product 1 - 5x Product 2 - 10x So, i want a function, that outputs the total of the order, like: Total products: 15x How can i store the code in an array and give the total order output? Thanks in advance! Bart
  11. Hello I have multiple images from a mysql database, I want to swap all these images onmouseover, IE, onmouseover, swap allthe images, each after some 3 4 seconds I amsure this will be done with javascript and probably jquery, So I need the Javascript (Or Jquery) function, and how to pass php parameters Naturally , there is a group of images, each will have more than one image to be swapped Many Thanks
  12. 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!
  13. Hello!!!! I have a while loop which outputs variables made from mysql query, but i want to order this output according to one of those php variables created later after the mysql query. I think that i can put all the outputted variables of the loop in an array and than order them in the array and output them. How can i make an array that will look like a matrix where nr.rows=nr.loops and nr.columns=nr.variables THAN order these rows by one column....like in excel: you create a table and than you sort them according to a column . After i output the array with a "for" loop for each row . PLS HEEELP. I know what i want but i don't know how can it be done EDIT: If your answers will be you can do it in mysql query.....than i say simply it's impossible......because the variable which i want to order the results from is generated AFTER the query and it's a mix of mysql variables($row[''])+form input variables ($_POST['']). So simply i cannot do it in mysql unless you suggest s.m.th else
  14. Hello: I am very new to programming and I am trying to create a program where I can enter fifteen different elements into this form and have the output be the fifteen elements I entered displayed in the order that which they were entered. <html> <head> <title>temp</title> </head> <body> <form action="temp.php" method="post"> <fieldset> Enter an integer <input type = "text" name + "temp0" size = 3 /><BR> Enter an integer <input type = "text" name + "temp1" size = 3 /><BR> Enter an integer <input type = "text" name + "temp2" size = 3 /><BR> Enter an integer <input type = "text" name + "temp3" size = 3 /><BR> Enter an integer <input type = "text" name + "temp4" size = 3 /><BR> Enter an integer <input type = "text" name + "temp5" size = 3 /><BR> Enter an integer <input type = "text" name + "temp6" size = 3 /><BR> Enter an integer <input type = "text" name + "temp7" size = 3 /><BR> Enter an integer <input type = "text" name + "temp8" size = 3 /><BR> Enter an integer <input type = "text" name + "temp9" size = 3 /><BR> Enter an integer <input type = "text" name + "temp10" size = 3 /><BR> Enter an integer <input type = "text" name + "temp11" size = 3 /><BR> Enter an integer <input type = "text" name + "temp12" size = 3 /><BR> Enter an integer <input type = "text" name + "temp13" size = 3 /><BR> Enter an integer <input type = "text" name + "temp14" size = 3 /><BR> <input type = "submit" vallue = "submit after entering 15 integers" /><br /> </fieldset> </form> </body> </html> What do I need to do? A helpful response would be greatly appreciated. Thank you much in advanced.
  15. Here is my mysqli query: SELECT `artwork`,`artist`,`title`,`label`,`year`,`price`,`id` FROM `products` WHERE (`qty` <> 0) AND (`agedOff` <> 1); I know this query works. I typed it into the mysqladmin window and it returned the desired results. Here is the variable setup & database query: $artwork = ''; $artist = ''; $title = ''; $label = ''; $year = 0; $price = 0.00; $id = ''; $vars = array(&$artwork, &$artist, &$title, &$label, &$year, &$price, &$id); //I placed the ampersands before the array values, because bind_results requires that you pass as reference if (!$stmt = $mysqli->prepare($query)) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } if (!call_user_func_array(array($stmt, 'bind_result'), $vars)) { echo "Binding results failed: (" . $stmt->errno . "( " . $stmt->error; } if($stmt->fetch) { //this part does not execute } The error message I receive is: "Notice: Undefined property: mysqli_stmt::$fetch in /hermes/waloraweb073/b2264/moo.katc/cobra/lib/distro.php on line 75 Fetching results failed: (0)" Can someone point me in the right direction please?
  16. Hi, I am trying to create an array from a mysql query that looks like this. $people = array( "8776546543" => "John Smith", "8779876543" => "John Dow", ); I need for that array to come from two columns of a mysql query and be placed on the $people variable. I am not sure how to do it. Can some one help me please?
  17. This is the first part of the search script and i was doing some debuging and i cant figure out why this code if( !($fd = fopen($url,"r")) ){ die( "Could not open URL!" ); } while( $buf = fgets($fd,1024) ) { /* Remove whitespace from beginning and end of string: */ $buf = trim($buf); /* Try to remove all HTML-tags: */ $buf = strip_tags($buf); $buf = preg_replace('/&\w;/', '', $buf); /* Extract all words matching the regexp from the current line: */ preg_match_all("/(\b[\w+]+\b)/",$buf,$words); print_r($words); } doubles each associate array like this Array ( [0] => Array ( ) [1] => Array ( ) ) Array ( [0] => Array ( [0] => It [1] = > was [2] => November ) [1] => Array ( [0] => It [1] = > was [2] => November ) ) When I would expect this Array ( [0] => Array ( ) ) Array ( [0] => Array ( [0] => It [1] = > was [2] => November ) )
  18. Hi, I have created an array with symbols that I'd like to allow in a string of text. $symbols[] = '>'; // Greater Than or Open Angle Bracket $symbols[] = '<'; // Less Than or Close Angle Bracket $symbols[] = '/'; // Forward Slash $symbols[] = '\\'; // Back Slash $symbols[] = '&'; // Ampersand $symbols[] = '£'; // Pound Sterling $symbols[] = '$'; // Dollar $symbols[] = '\"'; // Quotation Marks $symbols[] = '\''; // Apostrophe $symbols[] = '#'; // Hash $symbols[] = ' '; // Space $symbols[] = '.'; // Period I would now like to use something like str_replace or preg_replace (whichever is best) to replace any character that is not included within the array $symbols with "". I know there are other ways to filter characters but it isn't a suitable solution considering who will be maintaining it... :-\ I'm hoping for something like; but obviously a working way. $myText = str_replace(!$symbols, "", $myText; $myText = preg_replace(!$symbols, "", $myText; Thank you for your help.
  19. Hello dear friends, If i've the following array results then how i can show it up ! array code $urlByHost = array(); foreach (explode("\n", $_POST['url']) as $value) { $parse = parse_url($value); $urlByHost[$parse['host']][] = array( 'url' => $value, 'parse' => $parse, 'md5' => md5($value), ); } asort($urlByHost); print_r($urlByHost); results Array ( [www.bing.com] => Array ( [0] => Array ( [url] => http://www.bing.com/kee [parse] => Array ( [scheme] => http [host] => www.bing.com [path] => /kee_ ) [md5] => e69d3a5bb987448e30ec8559c3634caf ) ) i want to show the host and md5 as www.bing.com e69d3a5bb987448e30ec8559c3634caf how can i call the results and show it ! ~ thanks
  • 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.