Forum Moderators: coopster

Message Too Old, No Replies

Variables not setting in loop

         

itledi

11:18 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



I have an include at the top of my page which sets a template for a message used later.

Lower, I have a loop going through a MySQL table which gathers several variables such as content that will be used in that message.

Inside the loop is a function to output the combined text. My problem is that spaces inside the message where content from the table will be inserted into isn't going in, but stays blank.

I can't change the include page, but the only thing I can think of doing is putting the include inside the loop, making it look at that page each time the function loops, making it incredibly inefficient.

Any ideas? Thanks.

message.php
$message="You have ".$data["count"]." new messages.";

index.php
include($message.php);

$query=mysql_query("SELECT * FROM table");
while($data=mysql_fetch_array($query)) echo $message;

phparion

6:14 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if you need to show different number of messages based on each record of the table, then yes you need to put the include in the loop.

however, if you already got all information, i have doubt on this variable $data["count"], for your include and you are not counting any values from the query of while loop, then you should not include the message file in the loop...

one other thing I can think of is that your $data["count"] is static and you are counting more information to knit together with message file... for this whatever variables you do not want to count in the loop, can be made global scope and use in the loop. but do not declare them in the include instead make a separate include for them...

this is a very bad method to do it by the way. with OOP / classes you can organize it better by making member functions calls.

itledi

6:04 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



The $data["count"] would change each time the loop runs, as this comes from the MySQL table.

My problem is that I'm defining what my message is, including spots where I will insert data into later.

I get the data and I try to output the message expecting those spots to reflect its new value. The thing is, when I set what the message was in the first place, I used variables that weren't set yet, so I guess they equal null.

In the loop, I may set new values for parts of my message, but my message has already been defined with those unkowns set as blanks.

Will having to request that include inside the loop after the variable being set be my only option if I need to set the message on a seperate page? That just seems very intensive? But maybe it's not?

message.php
$message="You have ".$data["count"]." new messages.";

index.php
$query=mysql_query("SELECT * FROM table");
while($data=mysql_fetch_array($query)){
include($message.php);
echo $message;
}