Loop help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Loop help

Post 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)); ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

General Discussion is not where code questions/problems are posted about.


Moved to PHP - Code.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Oops :oops: Sorry :oops:

Any ideas about me query.
Ta
B
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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>&nbsp;</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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 :)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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". :roll:
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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. :x saying undefined index
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post by Sander »

That would be because that $_POST var didn't exist :wink:

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 :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Sami wrote:Define $i to 0 before the loop.
You can only define a constant, not a variable!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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;
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply