LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 05-01-2009, 12:48 AM   #1
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16
Thanked: 0
[solved] cleaner loop in php


[Log in to get rid of this advertisement]
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
pcock is offline  
Tag This Post , ,
Reply With Quote
Old 05-01-2009, 01:56 AM   #2
Acron_0248
Member
 
Registered: Feb 2006
Location: Venezuela
Distribution: Gentoo
Posts: 440
Thanked: 3
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 =)
Acron_0248 is offline     Reply With Quote
Thanked by:
Old 05-01-2009, 07:44 AM   #3
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 34
Thanked: 6
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
aspire1 is offline     Reply With Quote
Old 05-01-2009, 09:37 AM   #4
delite
Member
 
Registered: Nov 2008
Posts: 37
Thanked: 0
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
delite is offline     Reply With Quote
Old 05-02-2009, 01:59 PM   #5
haxple
LQ Newbie
 
Registered: Apr 2009
Posts: 2
Thanked: 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>';
?>
haxple is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


All times are GMT -5. The time now is 09:41 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration