If() action within print()..?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark

    If() action within print()..?

    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


  • Ian.H

    #2
    Re: If() action within print()..?

    On Sun, 04 Apr 2004 13:41:12 +0100, mark wrote:
    [color=blue]
    > 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[/color]


    <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


    Comment

    • mark

      #3
      Re: If() action within print()..?

      "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
      news:pan.2004.0 4.04.12.56.14.7 03000@bubbleboy .digiserv.net.. .
      [color=blue]
      > Do some reading.. then ask for advice if still applicable =)[/color]

      wow, fantastic..than ks 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.


      Comment

      • Ian.H

        #4
        Re: If() action within print()..?

        On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:
        [color=blue]
        > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
        > news:pan.2004.0 4.04.12.56.14.7 03000@bubbleboy .digiserv.net.. .
        >[color=green]
        >> Do some reading.. then ask for advice if still applicable =)[/color]
        >
        > wow, fantastic..than ks so much[/color]


        You're welcome =)

        [color=blue]
        > 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?),[/color]


        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).

        [color=blue]
        > I posted here figuring maybe someone could just help me.[/color]


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

        [color=blue]
        > 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.[/color]


        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!

        [color=blue]
        > 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.[/color]


        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


        Comment

        • Eric Bohlman

          #5
          Re: If() action within print()..?

          "mark" <dsffgdjg@jdsff ffgjghj.com> wrote in
          news:c4p16p$ssk $1@newsg4.svr.p ol.co.uk:
          [color=blue]
          > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
          > news:pan.2004.0 4.04.12.56.14.7 03000@bubbleboy .digiserv.net.. .
          >[color=green]
          >> Do some reading.. then ask for advice if still applicable =)[/color]
          >
          > wow, fantastic..than ks 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.[/color]

          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.

          Comment

          • mark

            #6
            Re: If() action within print()..?

            "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
            news:pan.2004.0 4.04.13.23.42.2 50000@bubbleboy .digiserv.net.. .[color=blue]
            > On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:[/color]
            [color=blue]
            > No.. you want ot be spoon fed. There's a difference.[/color]

            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.
            [color=blue]
            > 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).[/color]

            exactly, seeing as though I don't know what to search for the manual is no
            help at all. See my point? .....
            [color=blue]
            > I'm most definietly not saying you're not allowed to use it.. I _WANT_ you
            > to use it![/color]

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



            Comment

            • mark

              #7
              Re: If() action within print()..?

              "Eric Bohlman" <ebohlman@earth link.net> wrote in message
              news:Xns94C1572 9379FCebohlmano msdevcom@130.13 3.1.4...[color=blue]
              > "mark" <dsffgdjg@jdsff ffgjghj.com> wrote in
              > news:c4p16p$ssk $1@newsg4.svr.p ol.co.uk:[/color]
              [color=blue]
              > 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.[/color]

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



              Comment

              • Ian.H

                #8
                Re: If() action within print()..?

                On Sun, 04 Apr 2004 14:34:13 +0100, mark wrote:
                [color=blue]
                > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
                > news:pan.2004.0 4.04.13.23.42.2 50000@bubbleboy .digiserv.net.. .[color=green]
                >> On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:[/color]
                >[color=green]
                >> No.. you want ot be spoon fed. There's a difference.[/color]
                >
                > 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.[/color]


                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 ;)

                [color=blue][color=green]
                >> 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).[/color]
                >
                > exactly, seeing as though I don't know what to search for the manual is no
                > help at all. See my point? .....[/color]


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

                [color=blue][color=green]
                >> I'm most definietly not saying you're not allowed to use it.. I _WANT_ you
                >> to use it![/color]
                >
                > well, gimme a break. At least tell me what to seach for? or should I be
                > expected to know this already?[/color]


                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


                Comment

                • Andy Hassall

                  #9
                  Re: If() action within print()..?

                  On Sun, 4 Apr 2004 13:41:12 +0100, "mark" <dsffgdjg@jdsff ffgjghj.com> wrote:
                  [color=blue]
                  >I'm trying to do something like this:
                  >
                  ><?php
                  >foreach($apple s 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?[/color]

                  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 <andy@andyh.co. uk> / Space: disk usage analysis tool
                  http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                  Comment

                  • Geoff Berrow

                    #10
                    Re: If() action within print()..?

                    I noticed that Message-ID: <c4p332$9cj$1@n ews8.svr.pol.co .uk> from mark
                    contained the following:
                    [color=blue]
                    >"Eric Bohlman" <ebohlman@earth link.net> wrote in message
                    >news:Xns94C157 29379FCebohlman omsdevcom@130.1 33.1.4...[color=green]
                    >> "mark" <dsffgdjg@jdsff ffgjghj.com> wrote in
                    >> news:c4p16p$ssk $1@newsg4.svr.p ol.co.uk:[/color]
                    >[color=green]
                    >> 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.[/color]
                    >
                    >So you're saying I haven't earnt the right to know the answer? jeez...[/color]

                    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/

                    Comment

                    • mark

                      #11
                      Re: If() action within print()..?

                      "Andy Hassall" <andy@andyh.co. uk> wrote in message
                      news:1t40701vpb jhn47khoem64tmf 1i41g8gtg@4ax.c om...[color=blue]
                      > On Sun, 4 Apr 2004 13:41:12 +0100, "mark" <dsffgdjg@jdsff ffgjghj.com>[/color]
                      wrote:
                      [color=blue]
                      > You could split the string up, use the ternary operator on the middle[/color]
                      section,[color=blue]
                      > and concatenate the results.
                      >[/color]

                      thank you, that answered my question.


                      Comment

                      • Garp

                        #12
                        Re: If() action within print()..?


                        "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
                        news:pan.2004.0 4.04.13.45.38.4 22000@bubbleboy .digiserv.net.. .[color=blue]
                        > On Sun, 04 Apr 2004 14:34:13 +0100, mark wrote:
                        >[color=green]
                        > > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
                        > > news:pan.2004.0 4.04.13.23.42.2 50000@bubbleboy .digiserv.net.. .[color=darkred]
                        > >> On Sun, 04 Apr 2004 14:05:42 +0100, mark wrote:[/color]
                        > >[color=darkred]
                        > >> No.. you want ot be spoon fed. There's a difference.[/color]
                        > >
                        > > Don't assume I didn't look for the asnwer before I posted. I did, one of[/color][/color]
                        the[color=blue][color=green]
                        > > problems is half the time I don't know which terms to search for "php[/color][/color]
                        within[color=blue][color=green]
                        > > print " php within echo" print contains if" etc etc....see i'm not[/color][/color]
                        familiar[color=blue][color=green]
                        > > 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[/color][/color]
                        to be[color=blue][color=green]
                        > > able to do something (as fast as possible). I'm never going to master[/color][/color]
                        PHP,[color=blue][color=green]
                        > > 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[/color][/color]
                        tell[color=blue][color=green]
                        > > me the answer in 1% of the time it would take me to find it. Instead I[/color][/color]
                        find[color=blue][color=green]
                        > > myself drawn into this because I've some how offended you because I[/color][/color]
                        haven't[color=blue][color=green]
                        > > read the manual from start to finish.[/color]
                        >
                        >
                        > 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 ;)
                        >
                        >[color=green][color=darkred]
                        > >> Granted the manual can be a little confusing to a newcomer.. but once[/color][/color][/color]
                        you[color=blue][color=green][color=darkred]
                        > >> know what to search for, it is _the_ best resource (just trust me on[/color][/color][/color]
                        this[color=blue][color=green][color=darkred]
                        > >> one).[/color]
                        > >
                        > > exactly, seeing as though I don't know what to search for the manual is[/color][/color]
                        no[color=blue][color=green]
                        > > help at all. See my point? .....[/color]
                        >
                        >
                        > Well 'if' and 'print' may be good starts... no?
                        >
                        >[color=green][color=darkred]
                        > >> I'm most definietly not saying you're not allowed to use it.. I _WANT_[/color][/color][/color]
                        you[color=blue][color=green][color=darkred]
                        > >> to use it![/color]
                        > >
                        > > well, gimme a break. At least tell me what to seach for? or should I be
                        > > expected to know this already?[/color]
                        >
                        >
                        > 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/
                        >[/color]

                        Popcorn, anyone? <crunch>

                        Garp


                        Comment

                        • Sandman

                          #13
                          Re: If() action within print()..?

                          In article <c4ovos$rmc$1@n ewsg4.svr.pol.c o.uk>,
                          "mark" <dsffgdjg@jdsff ffgjghj.com> wrote:
                          [color=blue]
                          > 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[/color]

                          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]

                          Comment

                          Working...