Page 1 of 2
Loop help
Posted: Sat Aug 20, 2005 2:55 am
by Addos
I need to display <?php echo nl2br($row_getProperties['location']); ?> below on another part of my page in another separate table but as this will take <?php echo nl2br($row_getProperties['location']); ?> out of the loop it will only display the first record. Can anyone tell me how to get around this? I have tried running another loop but I get a conflict on the page and I have tried setting up a separate query and ‘where’ clause but again I end up with various results and none that are satisfactory. I thought that this would be easy enough but as a beginner I see I’m possible wrong.
Thanks for any advice.
B
Code: Select all
<?php do { ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tblproperty">
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="tblbg">Property</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['name']); ?></td>
</tr>
<tr>
<td class="tblbg">Location</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['location']); ?></td>
</tr>
<tr>
<td class="tblbg">Description</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['description']); ?></td>
</tr>
</table>
</td>
</tr>
</table>
<?php } while ($row_getProperties = mysql_fetch_assoc($getProperties)); ?>
Posted: Sat Aug 20, 2005 3:01 am
by feyd
General Discussion is not where code questions/problems are posted about.
Moved to PHP - Code.
Posted: Sat Aug 20, 2005 5:03 am
by Addos
Oops

Sorry
Any ideas about me query.
Ta
B
Posted: Sat Aug 20, 2005 5:23 am
by m3mn0n
Define $i to 0 before the loop. Then in the loop create an array like so $array[$i]['location'] and define the value of that key to the current looping var $row_getProperties['location']. Don't forget to add $i++ in there near the end of the loop so the number increases.
This should give you the ability to use all of the location variables the database calls anywhere on your page like so: $array[34]['location'] (that of course would be the 35th location that you extracted from the db).
Hope that helps.
Posted: Sat Aug 20, 2005 8:28 am
by Addos
Thanks so much for you help.
I’m really new to Arrays and Loops and I’m struggling hard to follow your good detailed explanation however I did try an few things and so far this is what I have come up with:
Code: Select all
<table>
<tr><td>
<p><strong>Search Found </strong>- </b><?php echo $totalRows_getProperties ?></p>
<p><strong>Facilities in </strong></p>
<p><!--HERE IS WHERE I WANT TO DISPLAY MY NEW LOCATIONS --></p>
</td><td >centre text</td><td>
<?php // if no entry is in db, don't show anything not even the table row
if (!empty($row_getProperties)) {
for ( $i = 0; $array[$i]['location'] = $row_getProperties['location']; $i++ )
do {
echo $array[9]['location'] ; ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="tblbg">Property</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['name']); ?></td>
</tr>
<tr>
<td class="tblbg">Location</td>
</tr>
<tr><!-- LOCATION HERE TO BE MOVED TO OTHER SPOT-->
<td><?php echo nl2br($row_getProperties['location']); ?></td>
</tr>
<tr>
<td class="tblbg">Description</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['description']); ?></td>
</tr>
<tr>
<td class="tblbg">Prices</td>
</tr>
<tr>
<td><?php echo nl2br($row_getProperties['prices']); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<?php } while ($row_getProperties = mysql_fetch_assoc($getProperties)); ?>
<?
} else {
echo ("Sorry there are no matches this time in our database. <br><br> <a href='javascript:history.back()'>Click here</a> to Search Again.");} ?>
</td>
</tr>
</table>
Now I’m sure it’s probably funny what I have tried but maybe if I’m close you can help me further. This at the moment is giving me “Notice: Undefined index: 9 in C:\Inetpub\wwwroot\appartments_php\TMPfgzeplivzx.php on line 372
['location']”
Many thanks once again
B
Posted: Sat Aug 20, 2005 9:40 pm
by raghavan20
not be really afraid of...its not an error...its a notice...
its a notice that says that particular variable is not declared.
you have to turn off notices...its enough if you have the warning and errors turned on
Code: Select all
error_reporting(E_ALL ^ E_NOTICE);
put this as the first line in all php files...it tells to displays all errors,warnings..except notices
Posted: Sat Aug 20, 2005 9:43 pm
by feyd
do not turn off notices. They are important and even when you "hide" them, they still fire. Fixing the problem is the best solution, by far.
Posted: Sat Aug 20, 2005 10:00 pm
by raghavan20
feyd, i am not really aware of other situations where notices might be of help.
if you dont mind, can you give me example situations where notices would help to identify holes in the code

Posted: Sat Aug 20, 2005 10:26 pm
by neophyte
Notices are useful. They tell you when variables are not defined, when indexes are not declared. I always put error_reporting(E_ALL); at the top of the script when I'm developing. The goal is to have the most bug-free secure code possible. Say it with me "E_ALL".

Posted: Sun Aug 21, 2005 5:40 am
by raghavan20
One time I found it the most annoying. It gave me a notice whereever I was using a POST variable for if block.

saying undefined index
Posted: Sun Aug 21, 2005 6:08 am
by Sander
That would be because that $_POST var didn't exist
You should do an isset() first, like this:
Code: Select all
<?php
if(isset($_POST['id']) && is_numeric($_POST['id']))
{
// blaat
}
?>
If you would only do the is_numeric part, it will give you an error when the var doesn't exist. If you first check it with isset, it won't

Posted: Sun Aug 21, 2005 6:27 am
by bokehman
Sami wrote:Define $i to 0 before the loop.
You can only define a constant, not a variable!
Posted: Sun Aug 21, 2005 8:03 am
by John Cartwright
bokehman wrote:Sami wrote:Define $i to 0 before the loop.
You can only define a constant, not a variable!
He meant $i = 0;
Posted: Sun Aug 21, 2005 8:14 am
by bokehman
Jcart wrote:He meant $i = 0;
Exactly. $i is a variable so it can have a value assigned but can't be defined.
Posted: Sun Aug 21, 2005 9:21 am
by feyd
although a bit off-topic, I'll say this:
"$i = 0;" is a variable definition, therefore it can be referred to as defining the variable.