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. 8 16979
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
"Adam Carolla" <ac****@carolla carpentry.com> wrote in message
news:BzW8d.876$ tU4.420@okeprea d06... 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($a rray);
$i=1;
foreach ($array as $item) {
if ($i==$count) {
print "Last item: ";
}
print $item . "<br />\n";
$i++;
}
- JP
"Adam Carolla" <ac****@carolla carpentry.com> wrote in message news:<BzW8d.876 $tU4.420@okepre ad06>... 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_arr ay($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
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
"Default User" <fi********@boe ing.com.invalid > wrote in message news:<I5******* *@news.boeing.c om>... 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 :)
Brad Shinoda wrote: "Adam Carolla" <ac****@carolla carpentry.com> wrote in message news:<BzW8d.876 $tU4.420@okepre ad06>... 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_arr ay($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?
Brad Shinoda wrote: "Default User" <fi********@boe ing.com.invalid > wrote in message news:<I5******* *@news.boeing.c om>... 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
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=mysq l_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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the form
title firstName lastName. The chapter that this assignment is on
deals with string arrays and includes pointers. I've seen how one can
reverse the order of letters in a word using pointers so thought maybe
I could discern the last word...
|
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 awhile?
Or is there any other way to implement this kind of a cache /
collection where one can do this kind of cleanup based on
least-recently-used objects?
|
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();
stack1.push_back(ptr);
|
by: tshad |
last post by:
I have a function:
function SalaryDisplay(me)
{
var salaryMinLabel = document.getElementById("SalaryMin");
salaryMinLabel.value = 200;
alert("after setting salaryMinLabel = " + salaryMinLabel.value);
}
I also have an asp.net object:
|
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 with the ADSI
NT Provider and only supported in the LDAP Provider.
So given an UserId (UID) how can I read the First Name and Last Name
using LDAP Provider. If anybody can help me with a C# sample code it
would of great help.
Thanks in advance.
| |
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
Public part1 as Integer
Public part2 as Integer
End Structure
|
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 ( yahooooo ) but i dont know why now I cant
get it to work the other way.
Vb 2003
Below are 2 examples. One Does not work and the other does.
|
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 show on only the
top half of each page with whitepace at the bottom of each page.
How can I get the detail to fill each page with my "half page height"
report footer at the bottom of the last page?
Any suggestions to solve this would be...
|
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 elements of the
same array object, or one past the last element of the array object,
the evaluation shall not produce an overflow; otherwise, the
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |