Log in

View Full Version : Newbie Question - Variables in PHP



Medyman
12-06-2007, 06:49 AM
Hi guys...

Simple question for most of u PHP geniuses out there...

I'm trying to do a very PHP intensive site. I figure it's the only way to really learn.

So, I've been trying to master some of the basics but can't seem to grasp them.

Here is the PHP declaration at the top of my page:


<?php
include 'php/config.php';
include 'php/opendb.php';

$query = "SELECT * FROM mission where section='1'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "{$row['content']}";
$c="{$row['content']}";

}

?>

the "echo" works fine. it echoes what i want it to.

Now, i'm trying to put the same thing that echoes into a variable so that i can use it elsewhere on the page. The data from the MySQL has to be integrated into the page, not just @ the top of the page.

I know there are better techniques then just echoing everything out.

How can I use the info from the content row and insert it into a textarea further down the page?

Also, what if I have another textarea to display info from the same MySQL table but "where section='2'". How would I go about coding that?

Thanks!!!

djr33
12-06-2007, 07:44 AM
With each loop, the variable $row is reset; as is, the variable $c is also reset in the same way. You can access $row or $c after this, but they will only have the value from the last rotation.

You will probably want to setup an array for this:


$i = 0;
while ($row = mysql_fetch_assoc($stuff)) {
$out[$i] = $row['stuff'];
$i = $i+i;
}

//and a cleaner way, but more complex:
for ($i=0;$row=mysql_fetch_assoc($stuff);$i++) {
$out[$i] = $row['stuff'];
}

However, rarely is this really needed.

Just going through the while loop at the right point should be enough in most cases. You should be able to place the while loop exactly where you need to output the text of each repetition.

Shotgun Ninja
12-06-2007, 06:34 PM
Use globals.

Instead of using $c, declare (earlier in the script):
global $c;

djr33
12-06-2007, 07:02 PM
Globals aren't related to this. A global variable is available within any scope (meaning it works in functions, basically), but it's value would still be overridden any time you reset it's value.


global $c;
$c = 1;
$c = 2;
//value of 1 is now lost.
//same as:
$c = 1;
$c = 2;

The solution is as I posted-- place the loop in an appropriate location on the page to generate the output, or use arrays.

One other method would be to create a string in the loop, like:

$string = '';
while () {
$string .= $row['addthis'];
}
That would add the value of $row['addthis'] with every loop, so if your first row returned was 1, the second was 3 and the third was 5, you would end up with a $string=='135'.

Medyman
12-07-2007, 05:37 PM
forgive me...but i'm still not understanding how to do this?

Here is the page as I have it:


<?php
include 'php/config.php';
include 'php/opendb.php';

$query = "SELECT * FROM mission where section='1'";
$result = mysql_query($query);

$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$out[$i] = $row['content'];
$i = $i+i;
}
?>

<html>
<head><title>TestPage</title></head>
<body>

<textarea><? echo $out[i] ?></textarea>

</body>
</html>



this isn't working so i'm doing something wrong. what is it?

boogyman
12-07-2007, 06:12 PM
<textarea><? echo $out[i] ?></textarea>


should be



<?php
foreach($out as $o) {
?>
<textarea><?php echo $o; ?></textarea>
<?php
}
?>


however if all your want is the content, there is no sense in pulling the rest of the data from the table.



$query = "SELECT content FROM mission WHERE section='1'";
$result = mysql_query($query);

$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$out[$i] = $row['content'];
$i += 1;
}

djr33
12-07-2007, 10:21 PM
There are many ways to do this. Those posted above will work, or perhaps this will make more sense.

The placement of PHP is easy-- put it where you want it to go. So,
<?php
include 'php/config.php';
include 'php/opendb.php';

$query = "SELECT * FROM mission where section='1'";
$result = mysql_query($query);
?>

<html>
<head><title>TestPage</title></head>
<body>

<textarea><?php
while ($row = mysql_fetch_assoc($result)) {
echo $row['content'];
}
?></textarea>

</body>
</html>