473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

last object of array

Hello,

I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".

Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.

Thanks in advance.
Jul 17 '05 #1
8 16960
Adam Carolla wrote:
Hello,

I have a script that is echo'ing the values of a mysql query using a
while. What I need to do is right before the last value is echo'd I
need to write "last item".

Is there a way to do this: figure out how many array values there are
then out put all but the last one, then write some text, then write
the last value of the array.

Either count() or sizeof() returns the number of elements in an array.


Brian
Jul 17 '05 #2
"Adam Carolla" <ac****@carollacarpentry.com> wrote in message
news:BzW8d.876$tU4.420@okepread06...
Hello,

I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to
write "last item".
Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last
value of the array.
Thanks in advance.


$count=count($array);
$i=1;
foreach ($array as $item) {
if ($i==$count) {
print "Last item: ";
}
print $item . "<br />\n";
$i++;
}

- JP
Jul 17 '05 #3
"Adam Carolla" <ac****@carollacarpentry.com> wrote in message news:<BzW8d.876$tU4.420@okepread06>...
Hello,

I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".

Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.

Thanks in advance.

$row = mysql_fetch_array($result);

for ( $i = 0; $i < count($row) - 1; $i++ )
{
echo $row[$i];
}
echo "last value";
echo $row[$i]; // not sure if $i or $i + 1, you'll have to check
Jul 17 '05 #4
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

You don't really want to call count() every single loop cycle, do you?


Brian
Jul 17 '05 #5
"Default User" <fi********@boeing.com.invalid> wrote in message news:<I5********@news.boeing.com>...
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

You don't really want to call count() every single loop cycle, do you?


Bleh, typing &~ thinking :)
Jul 17 '05 #6
Brad Shinoda wrote:

"Adam Carolla" <ac****@carollacarpentry.com> wrote in message news:<BzW8d.876$tU4.420@okepread06>...
Hello,

I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".

Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.

Thanks in advance.


$row = mysql_fetch_array($result);

for ( $i = 0; $i < count($row) - 1; $i++ )
{
echo $row[$i];
}
echo "last value";
echo $row[$i]; // not sure if $i or $i + 1, you'll have to check


As far as I know count returns the number of items in the array, which
are numbered 0 to count-1.

With these lines you print all items first, then 'last value' and then a
value that doesn't exist. You meant to write count($row) -2, right?
Jul 17 '05 #7
Brad Shinoda wrote:
"Default User" <fi********@boeing.com.invalid> wrote in message
news:<I5********@news.boeing.com>...
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

You don't really want to call count() every single loop cycle, do
you?


Bleh, typing &~ thinking :)


:)
I'm not sure how expensive the count() function is in PHP. Presumably
an array is some sort of object (although basically opaque) and
probably contains the current size in a member variable or somesuch,
like a C++ vector. Then count() would just return that value.

For a small array this probably makes little difference, but for a
large one every little bit helps, especially with a not particularly
speedy interpreted language.

For a compiled language, the compiler would probably examine the loop,
note that the array never changed, then optimize away the function call.

Brian
Jul 17 '05 #8

Adam Carolla wrote:
Hello,

I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I
need to write "last item".
Is there a way to do this: figure out how many array values there are

then out put all but the last one, then write some text, then write the
last value of the array.

How about using the function reverse_array()?

<? while($row=mysql_fetch_array($result)) {
$rev_row = reverse_array($row);
$next_to_last = $rev_row[1];
for ($i=0;$i<count($row);$i++) {
echo $row[$i]."<br>\n";
if ($row[$i] == $next_to_last) echo "Last Item<br>\n"; }
}
?>

The above hasn't been tested... YMMV ...

Ken

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bimo Remus | last post by:
Hi, I am currently taking a C++ class and am having problems with a homework assignment. My problem is that I need to pull the first and last words out of of a character string array which is in...
11
by: Vani Murarka | last post by:
Hi Everyone, Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for...
8
by: James Brown | last post by:
Hi, I am using the std::vector class as follows: vector <myclass *> stack1; and am pushing myclass objects onto the end of the vector like so: myclass *ptr = new myclass();...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
3
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work (...
13
by: Greg | last post by:
Most suggestions on this topic recommend to use a page footer and make it visible only on the last page. My problem is that the footer is half of the height of a page which means the detail would...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.