display one result from a set of db rows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramu528
    New Member
    • Mar 2008
    • 18

    display one result from a set of db rows

    Hi friends, I am working on two tables. one table(A) stores id of another table(B).

    I store id's in the table. with that corresponding 'id' ,i have to get the value given in table B. [php]<?php
    $query = 'SELECT * FROM jos_domain where userid='. $_SESSION['userid'];
    $db->setQuery($quer y);
    $rows = $db->loadObjectList ();
    foreach ($rows as $row)
    {

    $level= $row->lenglish_id;
    $db->setQuery("SELE CT * FROM jos_elevel where id= $level");
    $rows = $db->loadObjectList ();
    foreach ($rows as $row) { echo $row->english;
    echo ",";

    }
    ?>
    </td>
    </tr>[/php]the ouput is displayed has 2,3,4

    i want output to be displayed has only 2
    and when i request for rows[1] i need to display its corresponding value.
    then 3 has to displayed and then 4 has to be displayed.

    can anybody tell how to get a single value i.e., only 2 has to be displayed .
    when asked for it has to display value 3 . How ? plz. thanks in advance.

    regards,
    ramu
    Last edited by ronverdonk; May 7 '08, 11:27 AM. Reason: warning: use code tags!!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please provide a meaningful title to a thread. The one you used until now will not arouse much curiosity and attract many members.

    Thread Title changed to better describe the problem scenario.
    Please read here: Use a Good Thread Title
    ALSO:
    Enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    MODERATOR

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Since you use a foreach on all of the rows in the result set[php]foreach ($rows as $row) {
      echo $row->english;
      echo ",";[/php] it will display all values of all these rows. If you want one row only, then just fetch one row and display its value.

      If this does not help you with your problem, then re-state your question.

      Ronald

      Comment

      • Ramu528
        New Member
        • Mar 2008
        • 18

        #4
        Originally posted by ronverdonk
        Since you use a foreach on all of the rows in the result set[php]foreach ($rows as $row) {
        echo $row->english;
        echo ",";[/php] it will display all values of all these rows. If you want one row only, then just fetch one row and display its value.

        If this does not help you with your problem, then re-state your question.

        Ronald
        Hi Ronald,

        here , english is column in my table. the output is 2,4,5. it means 2 of from first row,4 from second row ,and 5 from third row. if i want to fetch only first row (english) column value , what should i change in code.echo $rows->english or echo $row->english is not working. can u help me out ? plz. thanks in advance.
        Ramu

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          The first row is index 0 in the $rows result set, so you have to get $rows[0], e.g.[php]$rows = $db->loadObjectList ();
          $row = $rows[0];
          echo $row->english;[/php]shows column of row 1 only.

          Ronald

          Comment

          • Ramu528
            New Member
            • Mar 2008
            • 18

            #6
            Originally posted by ronverdonk
            The first row is index 0 in the $rows result set, so you have to get $rows[0], e.g.[php]$rows = $db->loadObjectList ();
            $row = $rows[0];
            echo $row->english;[/php]shows column of row 1 only.

            Ronald
            Hi ronald,
            with the below code i am still retrieving all values from table(2 3 5)[php]foreach ($rows as $row)
            {
            $level= $row->lenglish_id;
            $db->setQuery("SELE CT * FROM jos_level where id= $level");
            $rows = $db->loadObjectList ();
            $row = $rows[0];
            echo $row->english;
            }[/php]here echo $row->english; is yielding same output . reading all values 2 , 3, 5. I am strucked here. plz any changes to be made. thanks ronald for the patience.
            Last edited by ronverdonk; May 7 '08, 12:54 PM. Reason: warning: code tags!!

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              THIS IS YOUR SECOND AND LAST WARNING:
              Please enclose your posted code in [code] tags (See How to Ask a Question).

              This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

              Please use [code] tags in future.

              MODERATOR

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                You cannot be that strucked. You use the variable $rows (and $row) twice in an nested foreach, so it gets all mixed up. Choose for the second SELECT different var names.

                Ronald

                Comment

                • Ramu528
                  New Member
                  • Mar 2008
                  • 18

                  #9
                  Originally posted by ronverdonk
                  You cannot be that strucked. You use the variable $rows (and $row) twice in an nested foreach, so it gets all mixed up. Choose for the second SELECT different var names.

                  Ronald
                  Problem With Nested Loop::::

                  Hi ronald, my code goes like this......[php]<?php
                  $query = 'SELECT * FROM jos_domain where userid='. $_SESSION['userid'];
                  $db->setQuery($quer y);
                  $rows = $db->loadObjectList ();
                  foreach ($rows as $row)
                  {

                  $level= $row->lenglish_id;
                  $db->setQuery("SELE CT * FROM jos_level where id= $level");
                  $rowtest = $db->loadObjectList ();
                  $rowt = $rowtest[0];
                  echo $rowt->english;
                  }
                  ?>[/php]
                  But i am getting same output. i am not getting desired output. plz help me out.I am getting all values from all rows..
                  Last edited by ronverdonk; May 7 '08, 02:27 PM. Reason: 3rd time: code tags!

                  Comment

                  • Ramu528
                    New Member
                    • Mar 2008
                    • 18

                    #10
                    Hi Friends, I didnt get the solution for nested loop. can anybody provide me with their code how to run a nested loop and retrieve particular column of a row in a table.plz.

                    thanks & regards,
                    ramu

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Since you keep ignoring all requests to follow the Posting Guidelines and enclose any code shown within appropriate code tags, this thread is closed.

                      moderator

                      Comment

                      Working...