[PHP] Problem with arrays
Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help
[PHP] Problem with arrays
Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help
[PHP] Problem with arrays
Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Problem with arrays
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help In this specific example, I think this would work: ? for ($i = 0; $i count($second_array); $i++) { array_push($first_array[$i], $second_array[$i]); } ? That's not terribly flexible, though. Is this used in a more generalized sense, or is it just this specific instance? -- Mike Johnson Smarter Living, Inc. Web Developerwww.smartertravel.com [EMAIL PROTECTED] (617) 886-5539 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem with arrays
Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
Mike Johnson wrote: From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Hi, I have 2 arrays: Array ( [0] = Array ( [0] = 28 [1] = Music ) [1] = Array ( [0] = 5 [1] = Books ) ) and Array ( [0] = aaa [1] = bbb ) I want to join this two array and the result must be loke this: Array ( [0] = Array ( [0] = 28 [1] = Music [2] = aaa ) [1] = Array ( [0] = 5 [1] = Books [2] = bbb ) ) Thanks in advance for your help In this specific example, I think this would work: ? for ($i = 0; $i count($second_array); $i++) { array_push($first_array[$i], $second_array[$i]); } ? That's not terribly flexible, though. Is this used in a more generalized sense, or is it just this specific instance? This variation of Mike's solution will allow the array keys to be non-numeric and/or non-incrementing. ?php foreach($second_array as $key=$value) { if(array_key_exists($key, $first_array)) { array_push($first_array[$key], $value); } else { print 'ERROR: Key '.$key.' does not exist in array $first_array.\n'; // or alternatively to add a new sub-array to $first_array // $first_array[$key] = array($value); } } ? --Bob -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
for ($i = 0; $i count($array1); i++) $array1[$i][] = $array2[$i]; from kevin l'huillier -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with arrays
On 24/06/05, Josh Olson [EMAIL PROTECTED] wrote: for ($i = 0; $i count($array1); i++) $array1[$i][] = $array2[$i]; from kevin l'huillier That's basically what Mike wrote (only with array_push instead of []), and Bob improved upon. And they didn't mix the arrays up. I was only making the point that the solution was simple with a quick comment in IRC. I had not intended for it to be posted to the list. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem with arrays
Hi, I have a form like this: form action=products.php method=post input name=position[pos][$value] type=text id=position[pos][$value] value=number 1 input name=position[pos][$value] type=text id=position[pos][$value] value=number 2 input name=position[pos][$value] type=text id=position[pos][$value] value=number 3 /form Note that $value from position[pos][$value] is different in all 3 fields. For each element of the array i want to update the value in the database. For example, for each value of the position[pos][$value] i want to update the database with the specific number. Something like this: $query = UPDATE table SET value = 'number 1' WHERE id='$value'; Thanks in advance for your help!!!
RE: [PHP] Problem with arrays
[snip] form action=products.php method=post input name=position[pos][$value] type=text id=position[pos][$value] value=number 1 input name=position[pos][$value] type=text id=position[pos][$value] value=number 2 input name=position[pos][$value] type=text id=position[pos][$value] value=number 3 /form Note that $value from position[pos][$value] is different in all 3 fields. For each element of the array i want to update the value in the database. For example, for each value of the position[pos][$value] i want to update the database with the specific number. Something like this: $query = UPDATE table SET value = 'number 1' WHERE id='$value'; [/snip] On Good Friday, when traffic is really slow, we have someone who wants to post their problem over and over again.cool Lets start simply, shall we? http://www.php.net/print_r -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fw: [PHP] Problem with arrays
I sent this to the original user, but forgot to CC it to the list. I'm just sending this now, for completeness. - Original Message - From: Jeff Schmidt [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, March 25, 2005 5:10 PM Subject: Re: [PHP] Problem with arrays Hello there. Well, let me start with a pointer to some documentation. I think you will find the following to be helpful: http://www.php.net/manual/en/control-structures.foreach.php In a nutshell, when your form is submitted, you will have present in the $_POST super-global array, a reference to an array representing position[pos]. (As an aside, why are you using the same value for the first key in each of the fields? What you will end up with is an array that looks like: array( pos = array(1 = number 1, 2=number 2, etc. . .)) Which seems like an extraneous level of nesting to me. . .) You can use foreach to walk through that array like so: foreach ($_POST['position']['pos'] as $key = $value) { //some code to update the database here. } NOTE: your $value from the form, in this case, corresponds to $key in the code above. That is, even though you are calling it $value in your original post, what you really are setting is a key in an associative array in PHP. Anyhow, back to the example code. This structure will loop through the array of form elements that were submitted, and each time it loops, it will set $key and $value to the submitted key and submitted value respectively, once for each text input. So, looking at your form below, let's say that the first text field has a $value of 1, second has $value of 2, etc. That is, the form gets sent to the user's browser like this: form action=products.php method=post input name=position[pos][1] type=text id=position[pos][1] value=number 1 / !-- etc -- /form The first time our foreach loop executes, $key will be assigned the value 1, and $value will be assigned the value number 1 (or whatever the user put into the text field, as they probably have changed it). So, bringing it all together, the code will look something like this: foreach ($_POST['position']['pos'] as $key = $value) { $key = mysql_real_escape_string($key); $value = mysql_real_escape_string($value); $query = UPDATE TABLE tablename SET value='$value' WHERE id='$key'; //code to actually execute the query goes here. } You can call the local values that foreach assigns to something other than $key and $value, but that is kind of a convention, and helps to keep the code clear.Also, I'd just like to throw out here, as a suggestion, in case you aren't already aware of this, that since user-input isn't trustworthy, before constructing your query string, you should make $key and $value 'safe' by using something like mysql_real_escape_string(), as in the example above. Hope this helps. Jeff Schmidt - Original Message - From: [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Friday, March 25, 2005 12:01 PM Subject: [PHP] Problem with arrays Hi, I have a form like this: form action=products.php method=post input name=position[pos][$value] type=text id=position[pos][$value] value=number 1 input name=position[pos][$value] type=text id=position[pos][$value] value=number 2 input name=position[pos][$value] type=text id=position[pos][$value] value=number 3 /form Note that $value from position[pos][$value] is different in all 3 fields. For each element of the array i want to update the value in the database. For example, for each value of the position[pos][$value] i want to update the database with the specific number. Something like this: $query = UPDATE table SET value = 'number 1' WHERE id='$value'; Thanks in advance for your help!!! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem using Arrays
Hi: I am trying to create a function that can store the user's info. Currently I have this: sessionInfo.inc === ?php class sessionInfo { /* * @(#) sessionInfoBean.java * * A class to handle sessions. * * @version 10 1 Oct 2003 * * */ /** * Variables */ var $last_name; var $first_name; var $userid; var $email_address; var $group = array(); function sessionInfo(){ } /** * Function to determine the group the user belongs to. * * @param groupIdChecks if the user is a member of the group. * */ function ismemberof($groupid){ for($i=0;$i=count($group);$i++) { if($group[i].equals($groupid)){ return true; } } } /** * Method to get the user id to establish a session. */ function getuserid(){ return $this -userid; } /** * Method that returns the available groups. */ function getgroups(){ return $this - group; } /** * Method to set the user id. * @param userid the user id. */ function setuserid($userid){ $this-userid = $userid; } /** * Method to add a new group. * @param group name of the group. */ function addgroup($groupid,$var){ $this -group[$var] = $groupid; } }; ? Now, in my login.php, I am trying to add the groups that the user belongs to, like this: login.php === $username = $_POST['username']; $password = $_POST['password']; //include the database connection file include 'include/dbConnection.php'; $query1 = select * from users where username='$username' AND password='$password'; $result=mysql_query($query1, $conn); //check that at least one row was returned $rowCheck = mysql_num_rows($result); if($rowCheck 0){ while($row = mysql_fetch_array($result)){ //start the session and register a variable $userid = $row['userid']; /** * Set the user's information. */ require 'include/sessionInfo.inc'; $sessionVar= new sessionInfo(); $sessionVar-setuserid($userid); } mysql_free_result($result); $query2 = SELECT group_id FROM user_groups WHERE userid=.$userid; $result = mysql_query($query2, $conn); //Get number of rows returned $numOfRows = mysql_num_rows($result); $i=0; while($row = mysql_fetch_array($result)){ $sessionVar-addgroup($row[group_id],i); while($inumOfRows){ $i .= 1; } } session_start(); $_SESSION['obj'] = $sessionVar; -- Now I redirect to a test page, where I am trying to print the no of groups the user belongs to. (As an example, every user belongs to atleast 3 groups). in Test.php = ?php require 'include/sessionInfo.inc'; session_start(); //get the user_id from sessionInfo. $sessionVar = $_SESSION['obj']; echo User id is :.$sessionVar-getuserid().br; echo User name is :.$sessionVar-getfirstname(). .$sessionVar-getlastname().br; $var = count($sessionVar-getgroups()); echo No of Groups User belongs to:.$var.br; ? Its gives this: User id is :7 User name is :Test Test No of Groups User belongs to:0 Any ideas, where I am going wrong ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problem with Arrays?
I ran into something interesting the only thing I can figure out is that functions won't use any variables other than globals, those past to the function, or those created inside the function? here's what I had that didn't print anything other than 3 blank lines for this section of code - $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; function getMaxSize( $maxSize ) { echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } but, if I put the array inside the function it would actually work - function getMaxSize( $maxSize ) { $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } what's up with this? Maybe I'm just up way to late again. Patrick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Arrays?
At 13:46 22.02.2003, Patrick Teague said: [snip] here's what I had that didn't print anything other than 3 blank lines for this section of code - $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; function getMaxSize( $maxSize ) { echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } [snip] if you declare $bytesize global within the function it will work: function getMaxSize( $maxSize ) { global $bytesize; echo $byteSize[0] . br/\n; } You'd be still better off passing the array as variable: function getMaxSize( $bytesize, $maxSize ) { echo $byteSize[0] . br/\n; } And lastly, for performance issues, pass it as a reference: function getMaxSize( $bytesize, $maxSize ) { echo $byteSize[0] . br/\n; } Without a reference, the array is copied to the function. By passing a reference the function is working on the original array, no copy overhead. Note: When passed as reference, any modification on the array within the function will effect the original array (same is true if declared global). Without reference the original array remains unchanged. -- O Ernest E. Vogelsinger (\)ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with Arrays?
on 22/02/03 11:46 PM, Patrick Teague ([EMAIL PROTECTED]) wrote: I ran into something interesting the only thing I can figure out is that functions won't use any variables other than globals, those past to the function, or those created inside the function? exactly :) you bring the array or variable into the scope of the function ? $byteSize[0] = bytes; $byteSize[1] = kb; $byteSize[2] = mb; function getMaxSize( $maxSize ) { global $byteSize; echo $byteSize[0] . br/\n; echo $byteSize[1] . br/\n; echo $byteSize[2] . br/\n; } ? Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] problem with arrays. Please Help!!
Hello, I need some help with checkboxes and multiple arrays, Here is the peace of code I am working with, and it gives me a list of products. This is the while statement which grabs information from the database: $tplv .= TR BGCOLOR=\$bgColor\ class=\medium\ align=\center\; $P.=input type=\checkbox\ name=\select[]\ value=\$id\ size=\3\; if($id == $select[$auctions_count]) $P .= CHECKED; $P .= ; $tplv .= TD width=\5%\$P/td; $tplv .= TD width=\15%\input type=\hidden\ name=\id[]\ value=\$q[id]\. $actual./TD; $tplv .= TD width=\15%\input type=\hidden\ name=\price[]\ value=\$q[price]\. $actual./TD; $tplv .= TD width=\15%\input type=\text\ name=\shipping[]\ value=\\ size=\10\; $tplv .= TD width=\10%\input type=\hidden\ name=\status[]\ value=\$q[status]\$stat1/TD; $tplv .= /TR; and creates someing like this checkboxesidpriceshippingstatus 11 4.99 1.99 0 245.99 2.990 As you can see I have 5 arrays. one is select /checkbox, id, price, shipping and status. What I am trying to do is search through the array of checkboxes and if the checkbox is checked I want the information in arrays id, price, shipping, status be added to the database. Ale there need to be a check if the status[] == 0; then add the information to a table. Can anyone help me to resolve the problem. Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]