Foreach not looping correctly |
Foreach not looping correctly |
masonh928 |
Sep 26 2015, 08:50 PM
Post
#1
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
CODE <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/Scripts/PHP/Library.php"); Class Board{ public $Connect; public $Poster; public $PostTo; public $Post; public $PostedDate; public $Poster2; public $PostTo2; public $Post2; public $Rows; public function __construct($PostToID){ try{ $this->Connect = new pdo("mysql:host=mysql.hostinger.co.uk;dbname=u645944288_all", "u645944288_mason", "mason671"); }catch(PDOException $ex){ die(json_encode(array('outcome' => false, 'message' => 'Unable to connect'))); } $Query = $this->Connect->prepare("SELECT Username FROM Users WHERE ID = :ID"); $Query->bindValue(":ID", $PostToID); $Query->execute() or die($Query->ErrorInfo()); $Row = $Query->fetch(); $this->PostTo = $Row['Username']; } public function DisplayPosts(){ $Query = $this->Connect->prepare("SELECT * FROM Posts WHERE Username = :Username ORDER BY Date DESC"); $Query->bindValue(":Username", $this->PostTo); $Query->execute() or die($Query->ErrorInfo()); $Results = $Query->fetchAll(); $this->Rows = $Results; /* foreach($Results as $Rows){ $Poster = $Rows['Poster']; $Date = AgeDate($Rows['Date']); $Post = $Rows['Post']; echo <<<Post <br><hr><br> <section style="color: #efefef;"> <h3>{$Poster}</h3> <h6>{$Date}</h6> <p>{$Post}</p> </section> Post; $this->Rows = $Rows; } */ } public function Test(){ print_r($this->Rows); } } ?> Whenever I echo $Rows['Poster'] for example, it just prints array. If I print_r() or var_dump() on the array itself to see if it'll print the items in the array, it just prints the word 'array'… I've seen the same problem on different forums, but no solutions just examinations. Obviously this means I'm not iterating correctly in the foreach, but I don't know where. Please help, thnx! Btw the reason I commented some of that out is so I could test print_r() the array of results. Var_dump() echo() print() print_r() All print same thing. "Array" This post has been edited by masonh928: Sep 26 2015, 09:03 PM |
CharlesEF |
Sep 26 2015, 11:30 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I think your $Results variable is a array of arrays. This means you will need 2 foreach loops to get to the data like you want. Try something like this:
CODE foreach($Results as $Rows) OR{ foreach($Rows as $key => $value) { // Place your other code here } } Try using a for loop instead. If you try this method then you would count the number of arrays in the first part of the variable. Something like this: CODE for($i=0; $i < count($Results); $i++) { // Access the info like this: $Results[$i]['Poster'] } This post has been edited by CharlesEF: Sep 26 2015, 11:42 PM |
masonh928 |
Sep 27 2015, 07:40 AM
Post
#3
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
How did you get that? I'm just wondering. Btw, $this->Rows = $Results is just for testing purposes, it's not actually used during displaying results. But thanks!
|
masonh928 |
Sep 27 2015, 07:56 AM
Post
#4
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Ok I tried using the second method a for() loop, and I get:
the word Array |
CharlesEF |
Sep 27 2015, 07:57 AM
Post
#5
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
How did you get that? I'm just wondering. Btw, $this->Rows = $Results is just for testing purposes, it's not actually used during displaying results. But thanks! |
CharlesEF |
Sep 27 2015, 07:59 AM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
|
masonh928 |
Sep 27 2015, 01:29 PM
Post
#7
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
When I print_r() the array, I get:
the word Array() Honestly, look here: http://www.social-board.96.lt/Profile?ID=27 (An example profile of my niece, scroll to bottom) |
masonh928 |
Sep 27 2015, 02:14 PM
Post
#8
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Is there any issue with the query, that may cause it to be a double-array, shall I say. It's the same technique, I've used time-after-time, I am greatly perplexed on this. There seems to be no reason to this. I've examined it with other code, and no difference, yet it works not.
|
CharlesEF |
Sep 27 2015, 02:28 PM
Post
#9
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Have you tried this approach:
CODE foreach($Results as $Rows) { print_r($Rows); } |
masonh928 |
Sep 27 2015, 02:32 PM
Post
#10
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
1 sec lemme see
|
masonh928 |
Sep 27 2015, 02:35 PM
Post
#11
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
CODE foreach($Results as $Rows){ print_r($Rows); /* $Poster = $Rows['Poster']; $Date = AgeDate($Rows['Date']); $Post = $Rows['Post']; $echo = <<<Post <br><hr><br> <section style="color: #efefef;"> <h3>{$Poster}</h3> <h6>{$Date}</h6> <p>{$Post}</p> </section> Post; */ } Prints out the word array. |
masonh928 |
Sep 27 2015, 02:40 PM
Post
#12
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
I'm at a loss…
I have done every technique in the book, to my knowledge, and all I get is array()… Let me go turn error reporting to strict and see if anything pops up. |
masonh928 |
Sep 27 2015, 02:42 PM
Post
#13
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
On turning it to E_STRICT and E_ALL I get:
Notice: Array to string conversion in /home/u645944288/public_html/Scripts/PHP/Class/Board.class.php on line 46 Array |
CharlesEF |
Sep 27 2015, 02:55 PM
Post
#14
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
On turning it to E_STRICT and E_ALL I get: Notice: Array to string conversion in /home/u645944288/public_html/Scripts/PHP/Class/Board.class.php on line 46 Array CODE foreach($Results as $Rows) I think you should see 8 items. Or, just fix the array to string problem.{ foreach($Rows as $line) { print_r($line); } } |
masonh928 |
Sep 27 2015, 02:56 PM
Post
#15
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Ok, for some reason I commented all of the code that prints anything on the page, yet it still seems to be the same thing. That means that the code isn't getting updated.
|
masonh928 |
Sep 27 2015, 02:57 PM
Post
#16
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Check my last post. This problem may be rooted elsewhere possibly. Though I will try your last test.
|
masonh928 |
Sep 27 2015, 02:59 PM
Post
#17
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
I have this and still prints array?:
CODE foreach($Results as $Rows){ echo("Hi"); //print_r($Rows); //var_dump(is_array($Results)); //var_dump(is_array($Rows)); /* $Poster = $Rows['Poster']; $Date = AgeDate($Rows['Date']); $Post = $Rows['Post']; $echo = <<<Post <br><hr><br> <section style="color: #efefef;"> <h3>{$Poster}</h3> <h6>{$Date}</h6> <p>{$Post}</p> </section> Post; */ } |
masonh928 |
Sep 27 2015, 03:03 PM
Post
#18
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Btw you requested full code here it is:
CODE <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/Scripts/PHP/Library.php"); error_reporting(E_ALL | E_STRICT); Class Board{ public $Connect; public $Poster; public $PostTo; public $Post; public $PostedDate; public $Poster2; public $PostTo2; public $Post2; public $Rows; public function __construct($PostToID){ try{ $this->Connect = new pdo("mysql:host=mysql.hostinger.co.uk;dbname=u645944288_all", "u645944288_mason", "mason671"); }catch(PDOException $ex){ die(json_encode(array('outcome' => false, 'message' => 'Unable to connect'))); } $Query = $this->Connect->prepare("SELECT Username FROM Users WHERE ID = :ID"); $Query->bindValue(":ID", $PostToID); $Query->execute() or die($Query->ErrorInfo()); $Row = $Query->fetch(); $this->PostTo = $Row['Username']; } public function DisplayPosts(){ $Query = $this->Connect->prepare("SELECT * FROM Posts WHERE Username = :Username ORDER BY Date DESC"); $Query->bindValue(":Username", $this->PostTo); $Query->execute() or die($Query->ErrorInfo()); $Results = $Query->fetchAll(); //$this->Rows = $Results; foreach($Results as $Rows){ foreach($Rows as $list){ print_r($list); } //print_r($Rows); //var_dump(is_array($Results)); //var_dump(is_array($Rows)); /* $Poster = $Rows['Poster']; $Date = AgeDate($Rows['Date']); $Post = $Rows['Post']; $echo = <<<Post <br><hr><br> <section style="color: #efefef;"> <h3>{$Poster}</h3> <h6>{$Date}</h6> <p>{$Post}</p> </section> Post; */ } } } ?> If you need the code where this class is used just ask. |
CharlesEF |
Sep 27 2015, 03:10 PM
Post
#19
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I would need to see the code in 'Board.class.php', or is the code you posted that class? Can you attach it? I 'assume' my last code, with the 2 foreach loops, did not print 8 items?
This post has been edited by CharlesEF: Sep 27 2015, 03:10 PM |
masonh928 |
Sep 27 2015, 03:11 PM
Post
#20
|
Serious Coder Group: Members Posts: 253 Joined: 17-August 13 From: Indiana Member No.: 19,570 |
Nope not at all
|
Lo-Fi Version | Time is now: 24th April 2024 - 05:36 AM |