peaforabrain Posted September 4, 2006 Share Posted September 4, 2006 Hi,I wonder if anyone could help me with this;I have two arrays, Array 1 is a list from A to Z, Array 2 is list of elements that contain data for a specific letter with a number indicating the amount of. So if 'C' for instance has two amounts of data and 'E' has one, then the list will show; A B C (2) D E (1) etc.I cannot get this to work correctly, please someone help. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 4, 2006 Share Posted September 4, 2006 Not totally sure what you want to achive....The 2nd array seems to have all teh info you need - merging it with an array that simply has a list of values that are the same as the keys of array 2 seems pointless. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 read up on arrays on http://php.net/arrayslook at array_diff and array_merge. Quote Link to comment Share on other sites More sharing options...
peaforabrain Posted September 4, 2006 Author Share Posted September 4, 2006 Thanks for the reply, but I have read a lot about arrays, and have tried various things.[quote] $result = array_merge($arr1, $arr2); $f = array_unique($result); while (list($indexValue, $fi) = each ($f)) echo"<option $sel value=\"".$fi."\">".$fi."";[/quote]This stops the duplication of elemnts, but I need to put the number next to the appropriate listing. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 [code=php:0]foreach($array as $key => $val) { echo "<option $sel value=\"".htmlentities($val)."\">".htmlentities($key).'. '.htmlentities($val)."</option>";}[/code] Quote Link to comment Share on other sites More sharing options...
peaforabrain Posted September 4, 2006 Author Share Posted September 4, 2006 Thanks Jenk,Here is where I am up to.[code=php:0] $f = htmlspecialchars($f); $arr1[]= "$f"; } } { if (!empty($a)) while(list(,$OneCount)=each($a)) { $count=$OneCount['name']; $num=$OneCount['num']; $arr2[]="$count"; $arr3[]="$num"; } { $result = array_merge($arr1, $arr2); $f = array_unique($result); while (list($indexValue, $fi) = each ($f))foreach($fi as $key => $val) { echo "<option $sel value=\"".htmlentities($val)."\">".htmlentities($key).'. '.htmlentities($val)."";}[/code]Could you point me in the direction of getting the number into the array?Thanks again. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 Which 'number'? The index is specified by the $key variable, do you mean something else, like item ID or similar? Quote Link to comment Share on other sites More sharing options...
peaforabrain Posted September 4, 2006 Author Share Posted September 4, 2006 Thanks, sorry for not being specific enough.The number I refer to is the amount of elements that are associated to a given field as extracted from the mysql db[quote] $count=$OneCount['name']; $num=$OneCount['num']; $arr2[]="$count"; $arr3[]="$num";[/quote]I havn't really shown it in this script as I could not get it to work, but I am presuming that making the number "$arr3" loop, then it should work. Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 $a is an array, where is this declared and defined? (i.e. can you post the loop that creates this array) Quote Link to comment Share on other sites More sharing options...
peaforabrain Posted September 4, 2006 Author Share Posted September 4, 2006 $a is defined from a function;[The other array is gathered from this; Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 ok, well it looks like you are going majorly overkill with your arrays then.Instead of creating so many arrays and doing all the shennanigans with them, just extract from the DB what you need - the image source link and the text by the looks of it.Then assign that directly to your template engine, then have the template iterate over the values outputting the HTML. Quote Link to comment Share on other sites More sharing options...
peaforabrain Posted September 4, 2006 Author Share Posted September 4, 2006 I know what you mean, and it is a lot easier to just extract from one list as mentioned by toonmariner.But I have a special need for this particular function and I actually do have it working (after a fashion because its random in its operation, not displaying the numbers as it should) using a lot of if else commands. If you can supply me with any poiters that would be great.thanks Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 4, 2006 Share Posted September 4, 2006 Well you've got your $result array returned by the function (which you've stowed away in var $a)so with $a, assign to the template.the template then iterates and decorates the data, ala my previous example where I assumed Smarty. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.