The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

5 Pages V  1 2 3 > »   
Reply to this topicStart new topic
> Foreach not looping correctly
masonh928
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 26 2015, 11:30 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,682
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)
{
  foreach($Rows as $key => $value)
  {
   // Place your other code here
  }
}
OR
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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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! tongue.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 27 2015, 07:57 AM
Post #5


Programming Fanatic
********

Group: Members
Posts: 1,682
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Sep 27 2015, 07:40 AM) *

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! tongue.gif
I have run into this problem before. In fact, I have created several arrays that do the same thing and required 2 foreach loops to access the data. Also, after getting some sleep I think there might be another way to get at your array data (I'm not sure how it works with an assoc. array). Use your original code as is but use the list command to assign the array to variables that you then can use.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 27 2015, 07:59 AM
Post #6


Programming Fanatic
********

Group: Members
Posts: 1,682
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Sep 27 2015, 07:56 AM) *

Ok I tried using the second method a for() loop, and I get:

the word Array

Can you post a small part of your print_r() results? So I can see the array structure.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 27 2015, 02:28 PM
Post #9


Programming Fanatic
********

Group: Members
Posts: 1,682
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Have you tried this approach:
CODE
foreach($Results as $Rows)
{
  print_r($Rows);
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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… sad.gif

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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 27 2015, 02:55 PM
Post #14


Programming Fanatic
********

Group: Members
Posts: 1,682
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(masonh928 @ Sep 27 2015, 02:42 PM) *

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
Have you looked at line 46 for problems? Without seeing all the code I can't say where the problem is. After looking at your code again and also looking at the sample page link you posted can you try this:
CODE
foreach($Results as $Rows)
{
  foreach($Rows as $line)
  {
    print_r($line);
  }
}
I think you should see 8 items. Or, just fix the array to string problem.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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;
*/
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 27 2015, 03:10 PM
Post #19


Programming Fanatic
********

Group: Members
Posts: 1,682
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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
masonh928
post 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 sad.gif

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

5 Pages V  1 2 3 > » 
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 12th December 2019 - 05:53 AM