Array_unique - How-to Get First Item Value In Repeated Array Item
I would like to know how-to get the first value of repeated items too in an array using something like array_unique. Code:
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Deleting An Array Item
I have a standard array: $foo = array( 0 => 'abc' 1 => 'def' 2 => 'ghi'); and I have a variable, $bar, which equals 'def' ... and I want to check my array to see if the value of $bar exists in the array, and if it does, delete it from the array. if (in_array($bar, $foo)) { // what goes here??? } I've done this before, but for the life of me, I'm drawing a blank now. Help a bruthah out?
Random Item Of An Array
What is the best way of picking out a random item of an array, regarding speed and CPU-usage ? I need a super fast way of picking out an ID from my MySQL db.
Shopping Cart Item Array
Trying to build a small simple shopping cart and I am having difficulty getting the solution. The cart is displaying each product, but it is overwriting the values. So if I have added 10 products, it shows ten items, but each item has the values of the last product I added. I think that this may be due to me trying to use a key to store both an array and string at once, but Iam not sure. I have looked at other examples but due to different methods, syntax is throwing me off. Code:
Remove A Single Item From An Array
I have gathered an array of ID's. I need to go throught the array and find a specific ID and remove it from the array. Because we want to keep that ID. Any suggestion of how to do that.
Arrays : Returning A Reference To An Array Item ??
I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist[] = new item("myitem"); //... and I am looking to develop a search function, which returns a reference to the found item. function &search_item($itemlist,"myitem") { //... } I am trying to figure out how to obtain a reference to a given item in an array. Indeed, foreach() and each() seem to work on copies of the data in the array. For example: $array = array("a","b","c","d"); print_r($array); foreach($array as $v) $v="xxx"; print_r($array); // $array is unchanged reset($array) ; while(list(,$v)=each($array)) $v="xxx"; print_r($array); // $array is unchanged, again
Deleting A Single Item From The Middle Of An Array?
I have an array that contain objects, I want to be able to test against the id of that object and remove it from the array. In it's simplest for I want to go from this: $people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo'); ...to this... $people = array('Tom', 'Dick', 'Brenda', 'Jo'); The fact that the items in the array are objects should not matter right? Plus I've already written my loops and conditionals to check which item i need to remove. function removeItem ( $item ) { $len = count ( $_SESSION['basket_arr'] ) ; if ( $len == 1 ) { emptyBasket ( ) ; } else { for ( $i = 0 ; $i < $len ; $i++ ) { if ( $item->id == $_SESSION['basket_arr'][$i]->id ) { //$_SESSION['basket_arr'][$i] must go } } }}
Removing Item From Array Then Overwriting File
I have a bookings.txt file which has a list of dates each on a new line, for example: - 21/06/07 22/06/07 29/06/07 25/12/07 01/01/08 I also have a variable $event_dd_mm_yy which I want to remove from this file if found. Code:
Iterator First Item?
Let's say I have the code: foreach($data as $row) { do something with each $row } Is there a way to do something specific to the first item iterated? Pseudocode: foreach($data as $row) { if first time through iteration do something with $row else do something which each remaining $row } Is this possible? I think I could use a regular 'for' loop, using the length of the $data array as a guide, and then an 'if..else' structure to see if it is the first time through the 'for' loop, but I was looking for a more elegant solution.
Trying To Split The Item...
I'm trying to make a search thru my mySQL database, so it any keywords are found then I'm adding the details to the next item in an array that I create. I've tested the array and it is storing the information into the array properly because it echoes out all of the information correctly. My problem is that, at the beginning of each item in the array I have information that tells me what part of the database it was pulled from and the id number and also how many times the a keyword was found in that specific database entry. So you have something like this.. Code:
Get Last Item In URL String
I realize how elementary this is, I apologize. Maybe that's why I can't find the right answer. I've tried everything from EXPLODEing the string to SUBSTR_REPLACE. Can anyone tell me how to just get the last item in a URL string? Like turn this: /mydirectory/myimg.jpg into this: myimg.jpg
Search Item In Database
I made a function that does a case insensitive search. This works fine. But i'd like to improve that script so that it also returns items from the database that are partly similar to the search string. this is the script atm:
Delete Item From Database
I still can't figure out how to delete an item from my shoppingcart. I have tried different ways to do this, but its impossible. -a button or link that deletes a item from my database -the php script that deletes it here is a suggestion: $slett = "DELETE ID, VARE, MODELL, STR, ANTALL, PRIS, TOTALPRIS FROM USER_TRACK WHERE USER_ID = "$user_id""; But how can i assign this to a button?
Updating Item Numbers (or IDs)
After a while of deleting records in a MySQL db, there gets to be the gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on. Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5, 6, etc without manually going in and changing those numbers?
Returning Autogenerated ID On New Item
Lets say you have a form that when saved to the mysql database creates a unique incremental id. The form is just the first part of a series of things the user can do, so once the ID is created, the page would need to know immediately what this ID is. Is there a way to grab this ID instantly once it is created? Obviously if this were a sign-up for and a new user name and password are involved, you can have the user login after the initial set up and this select statement would bring back the ID. But the situation I have doesn't work like that. The way I have been dealing with this is simply to have a select statement after the initial insert that requests the record based on a couple of other fields that are likely unique. But this seems like a rather kludgy way of doing it. Is there some other solution.
What Does `Catalog`.item Like '%A-%' Mean? -MySQL?
I am looking at an old query and I cannot remember or figure out what this part of the query means: `Catalog`.item like '%A-%' It is the like '%A-%' I cannot figure out. Then entire query is:[color=blue] > SELECT * > FROM `Catalog` > WHERE `Catalog`.img_status = Ƈ' AND `Catalog`.item_type ='R' AND > `Catalog`.item like '%A-%' > ORDER BY `Catalog`.item_type[/color] And will not work without the Like part
Pulling In One Item Of Four Categories
Ive got an offers table which contains name, category_id I want to display all offers, however the first page will only display four offers (one of each category). My query is as follows so how do I do this part? Code:
Finding # Of Item From Query
I have a query that goes "SELECT item_name FROM products WHERE products.drink = 'juice'" . If I get back 12 results from this query I can make a list using a simple loop, that is fine . If I click on one of these list items I want to go to a detail page, again I can do this no probs. How do I carry over to the detail page the number that the item appeared in the original list, for example if a result was "orange" and it was the 5th one in the list, how do I carry over the number 5 to the detail page?
Populating Form Item From SQL Enum Set Rev#2
$sql = "SHOW COLUMNS FROM table LIKE 'subject' "; $qry = mysql_query($sql) or die("Query not valid: " . mysql_error()); $res = mysql_fetch_object($qry); // This returns a row with a field 'Type' containing 'enum(...)' $res->Type = str_replace('enum('', '', $res->Type); $res->Type = str_replace('')', '', $res->Type); //now the string is something like this: one','two','three $temp = explode('','',$res->Type); //temp is an array with as much elements as the enum while ($temp) print array_pop($temp); This way is much more easier using the elements since they are elements of an array. Cleaver isn't it?
How To Replace An Item In The Url Search String
Using PHP 4, what is the most elegant way to replace a parameter value in a url query string? I want to replace the "Order" parameter, but it could occur anywhere in the query string. myfile.php?Param1=Hi+there&Order=ColumnA&Param3=Bye+Bye myfile.php?Order=ColumnA myfile.php?Param1=Anotehr+example&Order=ColumnA after applying this function, I'd like the result of the above examples to be myfile.php?Param1=Hi+there&Order=ColumnB&Param3=Bye+Bye myfile.php?Order=ColumnB myfile.php?Param1=Anotehr+example&Order=ColumnB
Add Item To Shopping Cart Problem
i have a cart link in all of my listed products on the site. when i press this link i will generate a url like this http://www.mysite.com/index.htm?loc...art&productid=3 after i press this link the site cart.php will be included and add the wanted product to the cart, and show the cart. my problem now is after the cart is showed with the new product in it, when i press the F5 key in the browser to refresh the site, the same product will added to the cart once more, because the previous generated url is unchanged in the browser. have anyone a idea how to fix this problem?
How To Apply Css To A Table That Is The Only Item In A Php File Please
How to apply css to a table that is the only item in a php file please In the page that has just the one table that is to be included in the other 2 pages, there's some heavy css going on, plus the text in the table gets changed quite a lot on a daily basis. Usually, in my program, I can highlught the text that needs a different css applied to it and click on the css style that applies to it, but because the page only has the one table and nothing else, there's no mention of the link to the styles page?
Language Depending Item Url's
I'm using a little newsflash item on my webpage, which is some text in an iframe. I have one newsflash item in English and one in another language, newsflash-english.html and newsflash-other.html How can I serve newsflash-english.html to english surfers and newsflash-other.html to not english users ? depending on the browser language ?
Redirecting To The ID Of Newest Item Created
I have a form that a user submits which creates a product. I want the user to be redirected to product.php?product_i=(the new product id) The problem i'm having is that i dont know the ID of the new product created because the table is set to auto increment. How would i go about finding out the new ID and redirecting to that new product after it has just been created?
Selected Item To Use For Multiple Variables
I've got a list of 'items' and those 'items' have unique 'categories'. I have 1 table for 'categories' that contains a list of category_ids and their 'category_names'. The other table is the 'item' list. And here's where I'm running into difficulty, I'd like to have the 'item' table contain the category_id from my 'categories' table as a Foreign Key and that category_id's 'category_name' in a 'category' field. I'm getting the Foreign Key, but I can't seem to grab the 'category_name' and assign it to my $category variable in the 'item' table. I've got a page to insert items and a page to update these items. I was working with the update first. And I'm able to grab the list of categories and select the correct one, but I'm not seeing where to assign the category_name to the $category variable. Here's dropdown list: Code:
Explicit Numeric Keys Vs Item Position
Say if I have a mixed array: $array = array("item1", "2"=>"item2", "5", "item4key"=>"item4") Is it possible while looping through the array (foreach ($array as $key=>$val)) to check if an item has an explicit key specified instead of its automatically assigned position key?
Choose Random Item From List But With Different Weighting
I want to choose an item at random, but have different probabilities for each item. One easy way is just to create an array like $a[]=1 $a[]=2 $a[]=3 $a[]=3 That way a random number between 1 and 4 would give the weighting I want, but it seems like there has to be a much smarter way to do this. I've seen some algorithms for random distributions, but they don't quite do what I want. Oh yeah - must not require a DB; this should work with values from a CSV or similar ...
Creating A Form For An Item Tracking Website
I'm having trouble processing the html form, especially the "date found" portion. I also don't really know how to set up my tables in my database so that they work efficiently. The information I an requesting with the form is: item_number, found_by, date_found, city, state, country. I'm assuming that I will have to have more than one table in order for people to search by item_number or found_by etc. I know these are kind of broad questions, but let me know if you'd like me to post what code I have, or stick the files on my FTP for download. I am using SQL Manager for a mysql client and Dreamweaver CS3 for design.
How To Remove Unwanted Words/characters From Database Item
currently i dump some data into our database from another database. now, i found some wierd characters  at the end of each of those items. and there are like 500 items in my database table which has those. is there a way that i can remove it using mysql query to be run in mysql or phpmyadmin something like update * from table where field name item has  and replace it with just blank? hope you could help me with this.
Listing Table Data With An Option To Delete Each Individual Item.
Was wondering if anyone could help me with a PHP, MySQL problem. I am completely new to PHP and MySQL. I have been trying to find a way to list data from a table on a web page and after each individual item have a hyperlink to allow users to delete the information listed. Can anyone help? i.e. Name Age Sex ------------------------------------------ Adam 21 M delete Betty 22 F delete Chris 23 M delete Daisy 24 F delete ------------------------------------------------------------------------------------------------------------------------------ (delete after 'Sex' would be the hyperlink to delete that entire line (row) from the table).
Array_unique Is Creating A NULL Array?
I'm writting a find and replace script for my sites internal search. Basically it is for the creation of a list of keywords that can later be fed through the search (kinda an advanced search of sorts)... Anyways, I've rewritten the function several times, and I think I am getting close, but there is one last snag. I am using array_unique() to remove the duplicates that are created by the function, and it is reutrning NULL. What's the deal? function findreplace() { extract($GLOBALS); if ($hold == 1) { return trim(stripslashes($keywords)); } $find = strtolower($_POST['find']); $replace = strtolower($_POST['replace']); $keywords = trim(strtolower($keywords)); //if ($_POST['replace_words'] == "Replace Words") { ....
Array_unique
I have a directory of files with filenames such as 20070704_A_01.pdf, 20070704_B_03.pdf, etc. I am trying to get the letter, such as A and B, exploded out of the filename. There will be several instances of the same letter, such as 5 letter A's and 6 letter B's, etc. I want to get a unique list of all of the letters. But when I use array_unique, it does not recognize that there are multiple instances of the same letter. The array still looks like: Code:
Array_unique
I have been working with the following section of code and each time I run my script I get an error that states my function arguument (for array_unique) is not a valid array. Could someone please assist me? I am not sure where the error could be, but I think I have a valid one dimensional array that should go through array_unique....
I Want To Do Array_unique BUT IN REVERSE!
hello everyone. This is a simple problem I think, but I can't work it out. Usually you need to get unique values from an array, but I actually sort of want to do the opposite to that. I need to get all the values from an array that appear more than once, and discard the rest, so like: (orange, banana, strawberry, strawberry, orange, pineapple, orange, orange) becomes (orange, strawberry) Any ideas how I would do that?
Implode, Keyword Distinct, And Array_unique?
I have an array of values for subcatid (1,2,3,4,5) called $subcatidarray I have a table called sf_subcategory where each row has the following columns, subcatid, catid, and name. I want to use each value in $subcatidarray to select the catid for that record and then I want to eliminate duplicate values (because subcatid's may share the same catids) then use the array of resulting (non-duplicate) catids to select name from a second table called sf_categories. I think I want to use either Distinct or the function array_unique but I'm stumped how to do this. this is what I've done so far, trying the DISTINCT keyword <?php $str = implode(",",$subcatidarray); $sql="SELECT DISTINCT catid"; $sql .="FROM $pntable[sf_subcategory] WHERE subcatid IN ($str)"; $result = mysql_query($sql); $str2 = implode (",",$result); $sql="SELECT name"; $sql .="FROM $pntable[sf_categories] WHERE catid IN ($str2)"; $result2=mysql_query($sql); ?>
Repeated Filling
How do I fill up a form continously and submit it remotely? Meaning, the form info comes in from a dbase using php and gets submited to a remote site. All this happens automatically.
A Repeated Region Within A Repeated Region...
I am trying to list each client in my database. The loop begins with the Clients table and starts spitting out information about the client. Then the second loop begins. This loop goes through the Projects table and it is suppossed to count the number of projects that belong to each member. Instead of looping through the Projects table each time the Client's loop loops through and gets information for the next client, the Projects table is only looped through on the first run-through of the first client. Can someone tell me how I can get the loop to start at the beginning of the Projects table each time the Clients loop starts over? Code:
I'm Getting The Same Row Of Info Repeated
I need to access data from a database and repeat the display for how many rows there are...Well I figured out the whole repeat thing I just can't make it go to the next row when it outputs... Code:
Repeated Forced Download In IE
I find this a very strange problem. I'm trying to force a download of a file, by linking to a PHP script and sending out http headers. When I click on this link in IE more than 2 times, it just won't work anymore. This on itself is not much of a problem, but I want to give the user the opportunity to download more than one file, and if I try listing 5 links, I can only download 2, and the rest won't work anymore. Is this just a nasty IE bug or am I forgetting something? Here's the code that forces the file to download:
File Select On Server (repeated)
(repeated because I forget to add comp.lang.php to the list) I have an admin interface. I want the admin to be able to select an image file on the server and attach it to an email to be sent out to a user. If I use type="file", it puts up a browse on the user's machine. Is there a comparable thing for use on the server? I could always place these images into a special directory and then simply have them listed in a select list. However, i was wondering if this more general thing is available. Of course, I would ONLY use it in an admin situation.
PHP - Passing A Variable From A Repeated Region?
I have a repeated region with a 'Name' a 'ID' and a 'Submit' button Repeated Region ------------------------------------------------ {Recordset1.Name}, {Recordset1.ID} | SUBMIT | ------------------------------------------------ so on my page I see every name next to every ID in my database and each one has their own submit button. Now what I need to do is on Submit pass the specific ID of the row in the table for which the submit button was pressed. For example if they scroll down to the 5 element in the repeated region (with ID 5) and they hit Submit I need to pass the number 5 to a function. I understand that you cant call a php function from a button press so I have been trying to do this with the if(isset($_POST['submit'])) { // do whatever with the variable. } statement. Does anyone know how I can do this properly?
Detect Phrase Being Repeated X Times In A Row?
Would somebody please tell me if there is a way to find out if something is repeated X times in a row in a string? Like if X is 5 and the string is "Cows go moooooooo" then it would replace the "ooooo" after "m" with Y "o"s, like "oo" if Y=2? Is this possible? And if it is could somebody please help me get started? I have no clue how to do this >_>
Selecting Array Rows Based On An Array Of Field=>values
Here's the code i have: $db = array(); //main array $db['main'] = array(); //table `main` $db['main'][] = array('id'=>0, 'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin'); //row $db['main'][] = array('id'=>1, 'username'=>'bob_smith','userpass'=>'psswrd','userlevel'=>'user'); //row function trim_value(&$value){ $value = strtolower(trim($value)); } function fdb_select($fields = "*", $table, $where = false, $limit = false){ global $db; $retv = $db[$table]; //LIMIT RESULTS TO FIELDS if(strrpos($fields, ",") === false){$field=array($fields);}else{$field = explode(",", $fields);}; array_walk($field, "trim_value"); $all_fields = array_keys($retv[0]); if($fields == '*'|| is_null($fields)){$field = $all_fields;}; $field = array_uintersect($all_fields, $field, "strcasecmp"); $retvi = array(); foreach($retv as $num =$row){ foreach($row as $fname =$fvalue){ $fname1 = strtolower($fname); if(in_array($fname1, $field)){ $retvi[$num][$fname]=$fvalue; }; }; }; //DONE LIMITING TO FIELDS if($where != false){ //UP TO HERE IT WORKS FINE //LIMIT TO WHERE CLAUSE $retvj = array(); foreach($retvi as $num =$row){ //IN THE ROW foreach($where as $k =$v){ //IF ROW MEETS ALL REQUIREMENTS if($row[$k] == $v){ $retvj[] = $retvi[$num]; //Keep that row }; }; }; //DONE LIMITING TO WHERE CLAUSE }; $retvi = (isset($retvj)) ? $retvj:$retvi; //SEND A FINAL ASSOCIATIVE ARRAY return $retvi; }; print_r(fdb_select("*", "main", array('id'=>"1", "username"=>"joshfrench"))); the previous code outputs this: Array ( [0] =Array ( [id] =0 [username] =joshfrench [userpass] =password [userlevel] =admin ) [1] =Array ( [id] =1 [username] =bob_smith [userpass] =psswrd [userlevel] =user ) )
|