LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2009, 12:48 AM   #1
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Rep: Reputation: 0
[solved] cleaner loop in php


Can someone help show me a cleaner way to do this please

Code:
<?php
// date menu
echo '<select name="date_part">'."\n";
$i = 1;
$ii = 1;
while ($i <= 31)
{
$date_menu .= '<option value="'.$i++ .'">';
$date_menu .= $ii++ .'</option>'."\n";
}
echo $date_menu;
echo '</select>';
?>

Last edited by pcock; 05-01-2009 at 03:08 AM. Reason: Solved
 
Old 05-01-2009, 01:56 AM   #2
Acron_0248
Member
 
Registered: Feb 2006
Location: Venezuela
Distribution: Gentoo
Posts: 453

Rep: Reputation: 33
The only thing that I would change is that $ii variable since it doesn't make sense

PHP Code:
$i 1;
while (
$i <= 31)
{
    
$date_menu .= "<option value=\"$i\">$i</option>\n";
    
$i++;
}

echo 
'<select name="date_part">'."\n";

echo 
$date_menu;

echo 
'</select>'
The same output can also be obtained using sightly different code so I think is more a matter of taste =)
 
Old 05-01-2009, 07:44 AM   #3
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
Can't really add much myself, apart from when I was learning programming it was drummed into us if you know how many times a loop is going to iterate use a for loop but I don't think it's a law or anything, you won't get arrested
 
Old 05-01-2009, 09:37 AM   #4
delite
Member
 
Registered: Nov 2008
Posts: 50

Rep: Reputation: 15
Code:
for($i=1;;$i++)
actually I forgot about the clause:
Code:
for($i=1;$i<=32;$i++)

Last edited by delite; 05-01-2009 at 09:39 AM. Reason: clause
 
Old 05-02-2009, 01:59 PM   #5
haxple
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
This gives you the exact same output

PHP Code:
<?php
echo '<select name="date_part">'."\n";
for (
$i=1;$i<=31;$i++)
    echo 
'<option value="'$i .'">'$i .'</option>'."\n";
echo 
'</select>';
?>
 
  


Reply

Tags
form, loop, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Text Cleaner nlavon Linux - Software 2 06-10-2008 07:32 AM
php + sql: endless loop problem konqi Programming 7 05-11-2008 10:02 PM
Good registry cleaner? colinstu General 5 07-24-2007 06:39 PM
PHP image-creation while loop taking forever benrose111488 Programming 9 07-09-2005 08:04 PM
MRU cleaner Ageless Linux - Software 0 04-26-2005 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration