Bluehost.com Web Hosting $6.95

next,key,current question

This is a discussion on next,key,current question within the PHP General forums, part of the PHP Programming Forums category; Sara, Try this: $count = count ($Classes); for($i=0; $i<$count; $i++){ $CurrentIndexValue = key($Classes); $CurrentContents = current($Classes); echo &...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-13-2003
Chris Hubbard
 
Posts: n/a
Default RE: [PHP] next,key,current question

Sara,
Try this:

$count = count ($Classes);
for($i=0; $i<$count; $i++){
$CurrentIndexValue = key($Classes);
$CurrentContents = current($Classes);
echo "<option value='$CurrentIndexValue'>$CurrentContents</option>";
next($Classes);
}

instead of the for loop you're using.

currently you're doing a next first, which will move you to the next element
in the array.
And then you're echoing the variables from the while loop, you should be
using the variables you've got in the for loop.

chris


-----Original Message-----
From: Sara Daugherty [mailto:sara@areawebdirectory.net]
Sent: Thursday, November 13, 2003 3:02 PM
To: php-general@lists.php.net
Subject: [php] next,key,current question


I am having trouble with creating a list box.
I am attaching the code.
I am not sure the code in the for loop close to the end of the program
is correct.

If someone could take a look at it I would appreciate it.

Thanks,
Sara
Reply With Quote
  #2 (permalink)  
Old 11-14-2003
Sara Daugherty
 
Posts: n/a
Default next,key,current question

I am having trouble with creating a list box.
I am attaching the code.
I am not sure the code in the for loop close to the end of the program
is correct.

If someone could take a look at it I would appreciate it.

Thanks,
Sara

<HTML>
<HEAD>
<TITLE>My Class Schedule</TITLE>
</HEAD>
<BODY background="http://www.areawebdirectory.net/sealmark2.gif">


<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Program Name:Class Schedule - pp05_01.php
//Programmer: Sara Gribble
//Due Date: October 27,2003
//Description: This program will several list boxes.
//Methods: The list boxes will be displayed using a
// Foreach loop
// While Loop - list and each functions.
// Manual Iteration - using the next, key and current functions
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This creates an associative array.
echo "<FORM ACTION='pp05_01a.php'>";

$Classes["DP237"] = "Programming Server Side Scripts";
$Classes["DP217"] = "Advanced CGI Programming";
$Classes["MA235"] = "Electronic Commerce";
// This funciton sorts the values in the array.
asort($Classes);

echo "<font color=#6A110C size=5>
<b><BR>This is using a while loop to create a list box.<BR></b></font>";

echo "<SELECT NAME='Classes'>";

// Here we use the while loop to display the values in the array in a list box.
while(list($ClassesKey,$ClassesValue) = each($Classes)){


echo "<option value='$ClassesKey'>$ClassesValue</option>";
}

echo "</select><BR>";

echo "<font size=5><b><BR>This is using a foreach loop to create a list box.</b></font>";

echo "<BR><SELECT NAME='Classes'>";

// Here we use a foreach loop to create a list box from the Classes array.
foreach ($Classes As $ClassesValue)
{

echo "<option value'$ClassesKey'>$ClassesValue</option>";
}

echo "</SELECT><BR>";
//The ksort function sorts the contents in the array by its indexes.
ksort($Classes);

echo "<font color=darkblue size=5><b><BR>This is creating a list box by manually iterating through an array.</b></font>";
echo "<BR><SELECT NAME='Classes'>";
// Here we will iterate through the array manually using a for loop and the next,current, and key functions.
for($i=0; $i<3; $i++){

echo "<option value='$ClassesKey'>$ClassesValue</option>";

next($Classes);
$CurrentIndexValue = key($Classes);
echo ($CurrentIndexValue);
$CurrentContents = current($Classes);
echo ($CurrentContents);
}
echo "</SELECT>";
?>
</BODY>
</HTML>

Reply With Quote
  #3 (permalink)  
Old 11-14-2003
Marek Kilimajer
 
Posts: n/a
Default Re: [PHP] next,key,current question

Sara Daugherty wrote:
> I am having trouble with creating a list box.
> I am attaching the code.
> I am not sure the code in the for loop close to the end of the program
> is correct.
>
> If someone could take a look at it I would appreciate it.
>
> Thanks,
> Sara


I like to use foreach:

foreach($Classes as $ClassesKey => $ClassesValue) {
echo "<option value='$ClassesKey'>$ClassesValue</option>";
}
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0