Search the Community
Showing results for tags 'arrays'.
-
Hi, Phreaks, I've made a number in inquiries into roughly the same problem. For that I apologize but I'm really confused and frustrated and can't work this seemingly simple problem out. Like before, I'm trying to make an associative array out of DB data and I can't work out some of the results I'm getting. If I use this code here -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { echo $item->id . " => " . $item->category . " "; } } I get this -> Excellent, so far But if I do this -> <?php public function all($column = "") { $items = array(); foreach($this->_results as $item) { $items [] = [$item->id => $item->category]; print_r($items); } } I get back a whole mound of data -> Why? I don't get it. Why is building this assoc array with category_id as the key and category as the value so difficult. I don't understand why this second code isn't making that arrray
-
I'm making a registration form and I'm really new at it. I've had a look about at similar issues but feel like I've got my code set up right, I obviously don't. I have a registration_handler.php and a functions.php file. I'm simply trying to pass an array ($errors[]) from the functions that generate them to a function that prints them out on registration_handler.php. It seems so easy, but I'm not getting it. Here's my code: from functions.php: (this is just an example of one of the functions) // USERNAME (MANDATORY) //uses the clean_names() and checks for empty input function assign_username() { global $conn; $username = $_POST['username']; if(!$username){ $errors[] = "A username is mandatory"; echo "A username is mandatory <br>"; } else { $username = clean_names($username); } //check for duplicate username and return $username if doesn't exist already $username_check = mysqli_query($conn, "SELECT username FROM users WHERE username='$username'"); $number_rows = mysqli_num_rows($username_check); if($number_rows > 0) { $errors[] = "Username already in use <br>"; echo "Username already in use <br>"; } else { return $username; } return $errors; } // END USERNAME Also from functions.php, the function to print out the error array: //display error array function show_errors($errors) { print_r($errors); } and the function call from registration_handler.php: if(isset($_POST['register_button'])) { //assign clean form variables $firstname = assign_firstname(); $lastname = assign_lastname(); $username = assign_username(); $email = assign_email(); $password = assign_password(); $date = date("Y-m-d"); $errors = array(); show_errors($errors); } I'm intentionally leaving the username out to test this on the form but I only get returned Array() It echos "Username is mandatory" but it seems like it's not passing the error string to the error array Can anyone point me towards what I'm doing wrong, please. I appreciate the replies
-
I am trying to get rows from MYSQL table (attachment) based on: Select last five rows where status_id = 1 Select last two rows where status_id = 2 *I did use UNION with two SELECT queries to get whole data as one array. Now on PHP side... I have first block for rows having status_id 1 so if there are rows having status_id 1 then PHP should display the data otherwise if there is no row having status_id 1 then PHP should print NO DATA only once. I have second block for rows having status_id 2 so if there are rows having status_id 2 then PHP should display the data otherwise if there is no row having status_id 2 then PHP should print NO DATA only once. I did use foreach loop then within loop i did use if condition to check status_id of rows. it works fine when i omit NO DATA part but when i add it. the result shows both rows and NO DATA in first block when there are one or more rows having status_id 1 but no row having status_id 2 Which made me believe that i was doing it the wrong way. Please guide me *PS: I know its PHP section the thread should be related to PHP problem but... is it right to use UNION in query? or should i post a new thread in relevant section for guidance?
-
Hi all, Need help in solving this task using PHP built in function : array_reverse() and without it. Appreciate it in explaining the code as I'm completely a newbie. function reverseArray($Arr) { } $fruits = ["Mango","Strawberry","Bananas","Pineapple"]; $result = reverseArray($fruits); Many thanks,
-
I dont understand arrays in PHP and its necessary to finally fess up and ask for help. It seems so trivial,, but I've spent Whole DAYS trying to make sense of this. (I came from assemble language programming). I am receiving UDP packets of a known size. in this case, 4 bytes of binary data. It gets unpacked ( I dont know why. I cut and pasted this simple parser from an example). All I want is the array[0] byte. I'm trying to avoid complex loops and pointers. // First lets grab a data packet and put our few bytes into the variable $buf while(1) { socket_recvfrom($socket, $buf, 16, 0, $from, $port); // Easy! but the UDP data in $buf is binary not useful like this. //Convert to array of decimal values $array = unpack("C*", $buf); // 'C' tells unpack to make unsigned chars from the binary. //Convert decimal values to ASCII characters: $chr_array = array(); // wait a second. What is happening here? for ($i = 0; $i < count($array); $i++) { $chr_array[] = chr($array[$i]); // Whats going on here? Anyone? } // So now I can assume the first byte of this $chr_array[] is my prize, right? $mychr = chr_array[0]; // Nope. Looking at $chr_array[0] either reveals a zero or the actual word "Array", depending on how late I've stayed up. print_r($array); // This is my debugger. It shows SOMETHING is working. echo "Received $mychr from remote $address $from and remote port $port" . PHP_EOL; } socket_close($sock); Output of above code... assuming the value read was 60 decimal: Array ( [1] => 60 [2] => 0 [3] => 0 [4] => 0 ) Received 0 from remote address nnn.nnn.nnn.nnn and remote port 8888 So print_r knows what to do, the last line of my code doesn't. Any help would be appreciated, as this goes to the heart of my issue of being mystified by how PHP makes and processes arrays. Its clearly not just a string of numbers. I'm trying to pull ONE printable number from that buffer. Halp!
-
Hi all, I am trying to echo reset($starttimes) to show the first value in my $starttimes array however i it is only showing the second value in the array and skipping the first i want to echo. The statement I have written is inside a while statement however I am not sure if that is where lies the problem. Please take a look at line 29 here: http://pastebin.com/2KQ7kgy9
-
I have to complete a multidimensional array, I have written PHP code but I keep getting numbers ontop of the arrays? Moreover; could you give me feedback on the multidimensional array is it anygood? <?php $customers = array( $customers =['Name' => 'Bob','id' => '587','Date' => '17/10/1015',], $customers =['Name' => 'Stu','id' => '100','Date' => '02/04/2010', ], $customers =['Name' => 'Kate','id' => '12','Date' => '09/12/2013',], ); $keys = array_keys($customers); $keys = array_keys($customers); for($i = 0; $i < count($customers); $i++) { echo $keys[$i] . "<br>"; foreach($customers[$keys[$i]] as $key => $value) { echo $key . " : " . $value . "<br>";} echo "<br>";} ?> //Ouput: 0Name : Bobid : 587Date : 17/10/1015 1Name : Stuid : 100Date : 02/04/2010 2Name : Kateid : 12Date : 09/12/2013
-
I have to complete a simple task which says 'use foreach to print the value of the sum of two cards, the deck has 16 cards and each card has one number and one colour'. Moreover; I can write four cards and four numbers, but how do I write all sixteen cards (I would prefer if they could sit abreast - parallel to each other)? The code I have so far is this: <?php $a = array( "Red" => 1, "Blue" => 2, "Green" => 3, "Yellow" => 4, ); foreach ($a as $key => $val) { echo "<h4>$key => $val.</h4>"; } ?> // Output: Red => 1. Blue => 2. Green => 3. Yellow => 4.