php


Issue with my array


Ok, well I have an issue. I want to set up a news feed, and this will show both questions, and comments that a user has received ordered by the time they were placed. I set up a function to do all of this. Again I don't know if it's the most efficient but I just want to get it to work. So this is what I have
public function foo ()
{
//Retrieve all of the questions
$Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?");
$Statement->execute(array($this->variable));
while ($row = $Statement->fetch(PDO::FETCH_ASSOC))
{
$id[] = $row["id"];
$question[] = $row["question"];
$asker[] = $row["asker"];
$timestamp[] = $row["timestamp"];
$likes[] = $row["likes"];
}
$iLimit = count($id); //Set a limit based on the size of the array
if ($iLimit > 0)
{
//Save everything in an array for the questions
for ($iCount = 0; $iCount < $iLimit; $iCount++)
{
$question[$iCount] = Array ( "id" => $id[$iCount],
"text" => $question[$iCount],
"username" => $asker[$iCount],
"timestamp" => $timestamp[$iCount],
"likes" => $likes[$iCount] );
}
}
//Retrieve all of the comments
$Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?");
$Statement->execute(array($this->variable));
while ($row = $Statement->fetch(PDO::FETCH_ASSOC))
{
$id[] = $row["id"];
$comment[] = $row["comment"];
$commenter[] = $row["commenter"];
$timestamp[] = $row["timestamp"];
$likes[] = $row["likes"];
}
$iLimit = count($id); //Set a limit based on the size of the array
if ($iLimit > 0)
{
//Save everything in an array for comments
for ($iCount = 0; $iCount < $iLimit; $iCount++)
{
$comment[$iCount] = Array ( "id" => $id[$iCount],
"text" => $comment[$iCount],
"username" => $commenter[$iCount],
"timestamp" => $timestamp[$iCount],
"likes" => $likes[$iCount] );
}
}
//Merge the two arrays
$aNewsFeed = array_merge($question, $comment);
foreach ($aNewsFeed as $row)
{
$aOrdered[] = $row["timestamp"];
}
array_multisort($aOrdered, SORT_DESC, $aNewsFeed); //Sort the array
return $aNewsFeed;
} //end getNewsFeed
then I call it using a foreach loop
foreach ($Class->foo() as $news)
{
echo $news["text"]
}
But every time it always gives two additional blank run-through's of the foreach loop. Or I don't know if it's actually part of the array, because usually it would send out an error if it was a problem with the foreach loop. I'm thinking it has to be a problem with the array_merge() function, but I'm not completely sure. Any ideas?
Thank you for the help. I really appreciate it.
It seems you are using the arrays $id, $likes, etc. as temporary variables but you are not resetting them before you use them the second time so your $id array will have questions in them when you start using it for the comments.
You should always initialize your arrays before you (re-) use them:
$id = array();
// first loop
$id = array();
// second loop
foreach ($Class->foo() as $news)
{
if($news["text"]!=""){
echo $news["text"];
}
}

Related Links

Symfony 2 - Can't get repository in private method - Call to a member function set() on null
Linux/Laravel - How to share uploads from different projects (same server) and show them on 1 subdomain?
Some memcahe functions are not working
Passing collection to email view using laraval 5.3 Notifications
cURL request in PHP
“Module (User) could not be initialized” : Exception in Zend Framework 2
Get languages OpenCart
Rewrite eats GET variables - QSA rule doesn't work
HTML and PHP send/return data [closed]
PHP return Logged In User
How to sanitize a Boolean with filter_var_array in PHP 7?
Ajax call never gets executed
Error handling for PHP under IIS: web.config still does not redirect when non-existing page is required
How can I confirm a PayPal payment was executed successfully?
mysql super user error: definer = root #localhost
PHP access array value with name

Categories

HOME
extjs
macos-sierra
ontouchlistener
focus
exchange-server
systemd
apk
google-play-services
cryptography
mirc
liferay-7
ontology
minecraft
hspi
angular4
xbap
fosuserbundle
word2vec
home
gitkraken
data-type-conversion
stackexchange.redis
cell
multi-dimensional-scaling
echosign
onchange
laravel-4.2
autodesk-model-derivative
objectmapper
windows2012
lxml
bootstrap-sass
char-pointer
ftp-server
jade4j
cargo
nanogallery
twilio-php
fastlane
suds
secure-coding
asihttprequest
jshint
destroy
ssi
sharpdx
simple-schema
1010
dex
emf-compare
sidewaffle
fitbit
yui3
rational-performance-test
bilinear-interpolation
conan
maven-release-plugin
adp
codecharge
timestamping
frame-grab
fdt
persistent-object-store
seadragon
ajax4jsf
grinder
bayesglm
fanotify
replicaset
scala-macros
text-align
mgwt
sablecc
maven-shade-plugin
mojo
jcheckbox
jolie
inputbox
loginview
rails-api
preorder
property-injection
strcmp
mute
lru
flv
eager-loading
spiral
arel
asp.net-mvc-3-areas
audiotoolbox
pcspim
monocross
multidrop-bus
space-partitioning
nosetests
sql-parametrized-query
printqueue
tabbarcontroller
j2mepolish
google-translator-toolkit
prism-2
account-management
parentid

Resources

Encrypt Message