By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

If() action within print()..?

P: n/a
I'm trying to do something like this:

<?php
foreach($apples as $key => $value){

print("<td class=<?php if ($myvar==$key){print (class1);} else {print
(class2);} ?>>$value</td>");

}
?>

Hopefully, you can see what I'm trying to do, I've left out all the ' \" '
etc. Obviously the syntax isn't right at all, how exactly do I achieve
putting an if() action with a print()? Is it even possible?

regards
Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Sun, 04 Apr 2004 13:41:12 +0100, mark wrote:
I'm trying to do something like this:

<?php
foreach($apples as $key => $value){

print("<td class=<?php if ($myvar==$key){print (class1);} else {print
(class2);} ?>>$value</td>");

}
?>

Hopefully, you can see what I'm trying to do, I've left out all the ' \" '
etc. Obviously the syntax isn't right at all, how exactly do I achieve
putting an if() action with a print()? Is it even possible?

regards

<http://php.net/>
Start reading the manual.

I was going to offer a solution... but this is way too basic a request to
just answer. You need to read and understand what you're doing (you have
no grasp at all on what you're doing.. no disrespect, I suspect you're new
to PHP) without being held by the hand.

Do some reading.. then ask for advice if still applicable =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
Do some reading.. then ask for advice if still applicable =)


wow, fantastic..thanks so much

Honestly. Yes, I terrible at PHP, I am not a programmer at all. I've spent
the last 30 minutes looking through google groups and found nothing (or
searched for the wrong terms maybe?), I posted here figuring maybe someone
could just help me. For someone who knows little about php the manual isn't
the easiest read in the world. But just because I know little doesn't mean
I'm not allowed to use it.

If you needed directions in a town you didn't know anything about, and
someone told you to go to the library to look up a map when you pulled over
to ask them I think you'd think they same about them as I'm thinking about
you right now.
Jul 17 '05 #3

P: n/a
On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
Do some reading.. then ask for advice if still applicable =)
wow, fantastic..thanks so much

You're welcome =)

Honestly. Yes, I terrible at PHP, I am not a programmer at all. I've spent
the last 30 minutes looking through google groups and found nothing (or
searched for the wrong terms maybe?),

Wow! A whole 30 minutes!?!?

I'v spent 6 years working with PHP and I still don't know it all (and
never will). As I said, _research_ (30 mins is a coffee break, that is
_not_ research).

I posted here figuring maybe someone could just help me.

No.. you want ot be spoon fed. There's a difference.

For someone who knows little about php the manual isn't
the easiest read in the world. But just because I know little doesn't mean
I'm not allowed to use it.

Granted the manual can be a little confusing to a newcomer.. but once you
know what to search for, it is _the_ best resource (just trust me on this
one).

I'm most definietly not saying you're not allowed to use it.. I _WANT_ you
to use it!

If you needed directions in a town you didn't know anything about, and
someone told you to go to the library to look up a map when you pulled over
to ask them I think you'd think they same about them as I'm thinking about
you right now.

No.. you're also asking them to tell you how to change direction in your
car.. how to move off.. how to indicate... how to......... get the picture?

If I get lost... I look into the A-Z I carry with me (READ: PHP manual)..
_then_ if I can't find what I'm after, I'll consider asking someone... but
I don't just use it as a cartoon flick-book.. I use the index.. I try to
locate what I want and where I am and go from there. You've skipped this
part it seems.

You'll see from my previous posts.. I'm more than happy to post examples,
help, advice to the best of my knowledge if I can... but you sir.. are
asking for the work to be pretty much done for you. This can be done
too... but please bear in mind that there would be an hourly fee involved.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4

P: n/a
"mark" <ds******@jdsffffgjghj.com> wrote in
news:c4**********@newsg4.svr.pol.co.uk:
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
Do some reading.. then ask for advice if still applicable =)


wow, fantastic..thanks so much

Honestly. Yes, I terrible at PHP, I am not a programmer at all. I've
spent the last 30 minutes looking through google groups and found
nothing (or searched for the wrong terms maybe?), I posted here
figuring maybe someone could just help me. For someone who knows
little about php the manual isn't the easiest read in the world. But
just because I know little doesn't mean I'm not allowed to use it.

If you needed directions in a town you didn't know anything about, and
someone told you to go to the library to look up a map when you pulled
over to ask them I think you'd think they same about them as I'm
thinking about you right now.


The problem here is that your question shows that you don't have enough
conceptual understanding of PHP to be able to understand a short answer to
your question. To give you an answer that you could actually use would
require a poster to write a very lengthy response that would involve quite
a lot of hard work. Conveying basic concepts to a beginner is an
inherently difficult task.

Fortunately, the team of volunteers who wrote the PHP manual put in that
effort. And rather than try to duplicate (usually nowhere near as well as
the original) that effort, people who know what they're talking about (that
is to say, the kind of people on whose expertise you're going to want to
call when you have a problem that *isn't* basic) are going to point you
toward the excellent results of that effort.

The problem with directly answering questions of the "tell me what to type
to make this happen" variety is that almost inevitably, the asker will come
back with another very elementary question about the answer he was just
given, because he doesn't understand what it means. This has been observed
over and over on just about every programming newsgroup. It makes for very
unproductive exchanges. Very early on in such an exchange, people start to
get the idea that the questioner is simply asking them to do his work for
him, and that makes the people who know what they're talking about drop out
of the exchange. And that in turn leaves the novice getting his "advice"
from people who may not in fact know what they're talking about.

And that brings up another important point: when you're a novice at a
subject, by definition you don't know enough about it to be able to tell
whether someone else knows what he's talking about. You have to go on
trust. And you can trust the authors of the official manual *far* more
than you can trust some random Usenet poster.

Jul 17 '05 #5

P: n/a
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote: No.. you want ot be spoon fed. There's a difference.
Don't assume I didn't look for the asnwer before I posted. I did, one of the
problems is half the time I don't know which terms to search for "php within
print " php within echo" print contains if" etc etc....see i'm not familiar
with it, hence it makes it alot harder for someone like me to find an
answer, a NINJA like you could just find it straight away. I just want to be
able to do something (as fast as possible). I'm never going to master PHP,
nor do I want to. From my point of view, I posted a simple question, one
that I assume many people on this forums know the answer to, and could tell
me the answer in 1% of the time it would take me to find it. Instead I find
myself drawn into this because I've some how offended you because I haven't
read the manual from start to finish.
Granted the manual can be a little confusing to a newcomer.. but once you
know what to search for, it is _the_ best resource (just trust me on this
one).
exactly, seeing as though I don't know what to search for the manual is no
help at all. See my point? .....
I'm most definietly not saying you're not allowed to use it.. I _WANT_ you
to use it!


well, gimme a break. At least tell me what to seach for? or should I be
expected to know this already?

Jul 17 '05 #6

P: n/a
"Eric Bohlman" <eb******@earthlink.net> wrote in message
news:Xn*******************************@130.133.1.4 ...
"mark" <ds******@jdsffffgjghj.com> wrote in
news:c4**********@newsg4.svr.pol.co.uk: The problem here is that your question shows that you don't have enough
conceptual understanding of PHP to be able to understand a short answer to
your question.


So you're saying I haven't earnt the right to know the answer? jeez...

Jul 17 '05 #7

P: n/a
On Sun, 04 Apr 2004 14:34:13 +0100, mark wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:

No.. you want ot be spoon fed. There's a difference.


Don't assume I didn't look for the asnwer before I posted. I did, one of the
problems is half the time I don't know which terms to search for "php within
print " php within echo" print contains if" etc etc....see i'm not familiar
with it, hence it makes it alot harder for someone like me to find an
answer, a NINJA like you could just find it straight away. I just want to be
able to do something (as fast as possible). I'm never going to master PHP,
nor do I want to. From my point of view, I posted a simple question, one
that I assume many people on this forums know the answer to, and could tell
me the answer in 1% of the time it would take me to find it. Instead I find
myself drawn into this because I've some how offended you because I haven't
read the manual from start to finish.

foreach ($apples as $foo => $bar) {
if (strncmp($myvar, $foo, strlen($myvar)) &&
(strlen($myvar) > 1) && (strlen($foo) > 1)
) {
echo 'class1;
} else {
echo 'class2';
}
}
DO NOT!!!! ask me to explain this to you... you obviously know already..
you spent 30 mins reading ;)

Granted the manual can be a little confusing to a newcomer.. but once you
know what to search for, it is _the_ best resource (just trust me on this
one).


exactly, seeing as though I don't know what to search for the manual is no
help at all. See my point? .....

Well 'if' and 'print' may be good starts... no?

I'm most definietly not saying you're not allowed to use it.. I _WANT_ you
to use it!


well, gimme a break. At least tell me what to seach for? or should I be
expected to know this already?

No one is expected to know anything.. but you're just not going about it
in the right way. 30 mins _DOES NOT_ constitute research!

There's a million and 1 examples of PHP code on the net and an if / print
statement has to be one of the most common combinations you're likely to
find.. yet you seem to be able to miss _ALL_ of them and ask us to code
for you.

As one of my old bosses once said to me:
Unfortunately, common sense isn't all that common.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #8

P: n/a
On Sun, 4 Apr 2004 13:41:12 +0100, "mark" <ds******@jdsffffgjghj.com> wrote:
I'm trying to do something like this:

<?php
foreach($apples as $key => $value){

print("<td class=<?php if ($myvar==$key){print (class1);} else {print
(class2);} ?>>$value</td>");

}
?>

Hopefully, you can see what I'm trying to do, I've left out all the ' \" '
etc. Obviously the syntax isn't right at all, how exactly do I achieve
putting an if() action with a print()? Is it even possible?


In short, no, you cannot have a conditional, or call any functions, within a
quoted string.

You could split the string up, use the ternary operator on the middle section,
and concatenate the results.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #9

P: n/a
I noticed that Message-ID: <c4**********@news8.svr.pol.co.uk> from mark
contained the following:
"Eric Bohlman" <eb******@earthlink.net> wrote in message
news:Xn*******************************@130.133.1. 4...
"mark" <ds******@jdsffffgjghj.com> wrote in
news:c4**********@newsg4.svr.pol.co.uk:

The problem here is that your question shows that you don't have enough
conceptual understanding of PHP to be able to understand a short answer to
your question.


So you're saying I haven't earnt the right to know the answer? jeez...


Pretty much. With the plethora of free code out there it is tempting to
just try to plug in PHP solutions. We've seen it time and time again.
But there really is no substitute for starting with the basic tutorials
and working your way through each of the constructs until you have some
idea of the basic logical processes.

This is a group for people who like to code in php. Are you really
surprised that someone who says " I just want to be able to do something
(as fast as possible). I'm never going to master PHP, nor do I want to."
get short shrift?

Make the investment in time - it will be worth it.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #10

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:1t********************************@4ax.com...
On Sun, 4 Apr 2004 13:41:12 +0100, "mark" <ds******@jdsffffgjghj.com> wrote:
You could split the string up, use the ternary operator on the middle section, and concatenate the results.


thank you, that answered my question.
Jul 17 '05 #11

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Sun, 04 Apr 2004 14:34:13 +0100, mark wrote:
"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@bubbleboy.digi serv.net...
On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:

No.. you want ot be spoon fed. There's a difference.


Don't assume I didn't look for the asnwer before I posted. I did, one of the
problems is half the time I don't know which terms to search for "php within print " php within echo" print contains if" etc etc....see i'm not familiar with it, hence it makes it alot harder for someone like me to find an
answer, a NINJA like you could just find it straight away. I just want to be able to do something (as fast as possible). I'm never going to master PHP, nor do I want to. From my point of view, I posted a simple question, one
that I assume many people on this forums know the answer to, and could tell me the answer in 1% of the time it would take me to find it. Instead I find myself drawn into this because I've some how offended you because I haven't read the manual from start to finish.

foreach ($apples as $foo => $bar) {
if (strncmp($myvar, $foo, strlen($myvar)) &&
(strlen($myvar) > 1) && (strlen($foo) > 1)
) {
echo 'class1;
} else {
echo 'class2';
}
}
DO NOT!!!! ask me to explain this to you... you obviously know already..
you spent 30 mins reading ;)

Granted the manual can be a little confusing to a newcomer.. but once you know what to search for, it is _the_ best resource (just trust me on this one).


exactly, seeing as though I don't know what to search for the manual is no
help at all. See my point? .....

Well 'if' and 'print' may be good starts... no?

I'm most definietly not saying you're not allowed to use it.. I _WANT_ you to use it!


well, gimme a break. At least tell me what to seach for? or should I be
expected to know this already?

No one is expected to know anything.. but you're just not going about it
in the right way. 30 mins _DOES NOT_ constitute research!

There's a million and 1 examples of PHP code on the net and an if / print
statement has to be one of the most common combinations you're likely to
find.. yet you seem to be able to miss _ALL_ of them and ask us to code
for you.

As one of my old bosses once said to me:
Unfortunately, common sense isn't all that common.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/


Popcorn, anyone? <crunch>

Garp
Jul 17 '05 #12

P: n/a
In article <c4**********@newsg4.svr.pol.co.uk>,
"mark" <ds******@jdsffffgjghj.com> wrote:
I'm trying to do something like this:

<?php
foreach($apples as $key => $value){

print("<td class=<?php if ($myvar==$key){print (class1);} else {print
(class2);} ?>>$value</td>");

}
?>

Hopefully, you can see what I'm trying to do, I've left out all the ' \" '
etc. Obviously the syntax isn't right at all, how exactly do I achieve
putting an if() action with a print()? Is it even possible?

regards


Basically, what you're trying to do here is print a string which itself will
break out to be php parsed. It doesn't work that way. Here is how it should be
done:

<?
foreach ($apples as $key => $value){
$class = $myvar == $key ? "class1" : "class2";
print "<td class='$class'>$value</td>\n";
}
?>

--
Sandman[.net]
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.