php question. Pls take a look and see if you can help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • needaticker@mail.com

    php question. Pls take a look and see if you can help

    Hi All,

    I've created a new table (eg/ 'XM_newtable') on my database and in it I
    have a single field (eg/ 'newfield')

    I have entered 100 values under the 'newfield' and what I need to do is
    display these values at random wherever I insert a bit of code.

    My problem is that I don't know what bit of code to insert


    I guess that it will be a code similar to below. But what would the
    direct path to this field be? And how can I make the result be random
    from this field?

    <?php
    print("$newfiel d");[color=blue]
    >[/color]

    The final thing that I would like is that if the field doesn't have any
    entries (so if I take out the 100 values that I've put in) then I would
    like to have the code print the message "sorry but no value available!"


    All help is always appreciated.


    Thanks

  • Darkstar 3D

    #2
    Re: php question. Pls take a look and see if you can help

    I use a random number to do things like this. Put in a sort field and
    put a random number from 1 to 100 in it. Parse the records and sort
    according to that number.

    Comment

    • Darkstar 3D

      #3
      Re: php question. Pls take a look and see if you can help

      Wish I had googled before I wrote mine. Nah, it helped me.

      If you are in a hurry however, take a look at this.



      Comment

      • IWP506@gmail.com

        #4
        Re: php question. Pls take a look and see if you can help



        This URL can show you how to access records, print them out, loop
        through, and display an error if none exist.

        I would just have an ID field that's auto-increment from 1-100 then
        generate a randon number and print out the results from the field with
        the ID corresponding to the randomly generated number.

        Comment

        • needaticker@mail.com

          #5
          Re: php question. Pls take a look and see if you can help

          thanks for the quick reply. I'm still a little confused.

          i am such a newb to this. the link seems to include a lot more that's
          needed. I'm going to have another read through the script again to see
          if I can use any of it.

          One thing is that the number of entries in the fields will be
          constantly changing as other php pages are adding entries and taking
          some away and so I'm not to sure how to do the random bit if the total
          number of entries are always changing.

          Thanks for your help

          Comment

          • needaticker@mail.com

            #6
            Re: php question. Pls take a look and see if you can help

            thanks guys, i am really learning something tonight.

            piece by piece I'm getting there.

            Comment

            • Zoe Brown

              #7
              Re: php question. Pls take a look and see if you can help


              <needaticker@ma il.com> wrote in message
              news:1126379966 .540098.49470@g 14g2000cwa.goog legroups.com...[color=blue]
              > thanks for the quick reply. I'm still a little confused.
              >
              > i am such a newb to this. the link seems to include a lot more that's
              > needed. I'm going to have another read through the script again to see
              > if I can use any of it.
              >
              > One thing is that the number of entries in the fields will be
              > constantly changing as other php pages are adding entries and taking
              > some away and so I'm not to sure how to do the random bit if the total
              > number of entries are always changing.[/color]

              do a count first so that the script know how many numbers /entries there are
              and the selects a number in that range.


              Comment

              • Darkstar 3D

                #8
                Re: php question. Pls take a look and see if you can help

                Do your program logic on paper or flow chart before you code. Will save
                yourself some time, or at least it does me. All about planning.

                Comment

                • needaticker@mail.com

                  #9
                  Re: php question. Pls take a look and see if you can help

                  Thanks for your help so far, I think that I'm getting there and have
                  come up with what follows which isn't working but maybe you can spot
                  what's wrong with it.

                  The basics is that I have a table on the database that has a single
                  field (i have been trying some other stuff which includes an ID number
                  field but I wondered if anyone can help me fix the stuff below so I
                  don't need it).

                  The single field has about 100 entries so far but this will always
                  change and at some times there may be no entries (see the if / else
                  clause).

                  Otherwise I need to display a random entry from the table.

                  Here goes...



                  <?php
                  $username="dbus ername";
                  $password="dbpa ssword";
                  $database="dbna me";

                  mysql_connect(l ocalhost,$usern ame,$password);
                  @mysql_select_d b($database) or die( "Unable to select database");


                  $chooseall="SEL ECT * FROM tablename";
                  $chooseallresul ts=mysql_query( $chooseall);
                  $numrows=mysql_ numrows($choose allresults);

                  $randget="SELEC T * FROM tablename ORDER BY RAND() LIMIT 1";
                  $randgetresults =mysql_query($r andget);

                  if ($numrows=0) {echo "Sorry no entries"
                  } else {
                  echo "$randgetresult s"


                  mysql_close();
                  ?>

                  Comment

                  • needaticker@mail.com

                    #10
                    Re: php question. Pls take a look and see if you can help


                    This pretty much does what I need it to but it adds some code in the
                    result. (I mean instead of just showing the "random entry", it shows
                    "Array ( [field] => random entry )". It would be great if I could get
                    rid of all the other stuff. Here's my code so far. Can anyone tidy up
                    my result to get it to do what I want it to?

                    Thanks

                    <?php
                    $username="user name";
                    $password="pass word";
                    $database="data base";

                    mysql_connect(' localhost',$use rname,$password );
                    @mysql_select_d b($database) or die( "Unable to select database");

                    $randgetq=mysql _query("SELECT * FROM tablename ORDER BY RAND() LIMIT
                    1");

                    if (!$randgetresul ts=mysql_fetch_ array($randgetq , MYSQL_ASSOC))
                    {
                    echo "Sorry no entries";
                    }
                    else
                    {
                    print_r($randge tresults);
                    }
                    mysql_close();
                    ?>

                    Comment

                    • needaticker@mail.com

                      #11
                      Re: php question. Pls take a look and see if you can help

                      Someone just helped me with the answer so I thought that I'd let
                      everyone else know (incase anyone else in future wants to know how to
                      do this)


                      The Answer:

                      Replace "print_r($randg etresults);"

                      with "echo "$randgetresult s[token]";"

                      Thanks for all your help.

                      Comment

                      • Darkstar 3D

                        #12
                        Re: php question. Pls take a look and see if you can help

                        Print would also work, Print_r prints the value

                        Comment

                        • Adam i Agnieszka Gasiorowski FNORD

                          #13
                          Re: php question. Pls take a look and see if you can help

                          On 2005-09-10 21:36:48 +0000, "Darkstar 3D" <stacyspear@gma il.com> said:
                          [color=blue]
                          > Do your program logic on paper or flow chart before you code. Will save
                          > yourself some time, or at least it does me. All about planning.[/color]

                          You mean dead trees? How about OmniGrafle?

                          -- 
                          Seks, seksiæ, seksolatki...<u ri: news:pl.soc.sek s.moderowana > <~|{ A.A }|
                          Love, give me a reason to be beautiful! Miles and miles of perfect skin,
                           I said I fit so perfect in! I'm fading like a rose...Give me a reason!!!
                          https://hyperreal.info | https://kanaba.info |=> "Go¶ciu! Szanuj Zieleñ!"

                          Comment

                          • Darkstar 3D

                            #14
                            Re: php question. Pls take a look and see if you can help

                            Paper works best for some, programs work for others. Programs work
                            great for me, mainly Visio, but lately I have been using FreeMind.

                            Comment

                            Working...