SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot p2ranger's Avatar
    Join Date
    Jan 2001
    Location
    Colorado, USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure this is a fairly simple question, but I can' figure it out.

    I want to be able to run a function and after it has run, pass the variables out of the funtion to be used with in the rest of the PHP program. I'm assuming that while works just like any other function.

    More specifically, I am using if and while to check on data in a MySQL database. If the data in the query in one column is 0 then it runs the while funtion. The while function checks the next row until it finds a row that is not 0 in that column. This row id then needs to be passed out side of while and if to the rest of the program.

    Thanks
    Jason
    ><>

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IF and WHILE are NOT functions.
    They're control structure of a language (PHP)
    So I don't really understand what you want to do.
    For a normal function:

    function returnHello()
    { return "Hello";
    }
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Zealot p2ranger's Avatar
    Join Date
    Jan 2001
    Location
    Colorado, USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so they aren't functions, thats the answer to one question. Thanks

    Let me try and clear up the question

    I am using a while loop to evaluate the value in the column of a MySQL table. The loop runs from one row to the next until it comes to a row that does not have the value of 0 in the specified column. I then want it to return that row to the program.

    It is supposed to behave like a webring.

    Code:
     
    
    <?php
    
    
    // connect to the database server
    $dbcnx = @mysql_connect("localhost", "root", "pass");
    if (!$dbcnx)
    {
        echo( "<p>Unable to connect to the database server at this time.</p>");
        exit();
    }
    
    // select the philring database
    if (! @mysql_select_db("philring") )
    {
        echo( "<p>Unable to conect to the database at this time</p>");
        exit();
    
    }
    
    //get the number of rows in the table (gets the numer of 
    //sites in the ring)
    
    $result = mysql_query("select * from philring");
    
    $number_of_sites=mysql_num_rows ($result);
    
    if (@$direction == "next")
    {
        $id++;
    }
    
    if (@$direction == "previous")
    {
        $id--;
        if($id < 1)
    
        $id=$number_of_sites;
    
    }
    
    
    if ($id > $number_of_sites)
    {
        $id=1;
    }
    
    //slect url and status from database
    
    $result = mysql_query("select url, status from philring where id=$id");
    if (!$result)
    {
        echo("<p>Error performing querry: " . mysql_error() . "</p>");
        exit();
    }
    
    
    $row=mysql_fetch_array($result);
    
    //check to see if selected site has active status or not
    //status = 0 is inactive and status = 1 is active
    
    
    if ($row[1] == 0)
    {
      while ($row[1]=0)
        {
            $id++;
            $result = mysql_query("select url, status from philring where id=$id");
            $row=mysql_fetch_array($result);
        }
    }
    
    echo("<meta http-equiv=\"refresh\" content=\"0;URL=$row[0]\">");
    
    ?>

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, so you would you use break;
    Eg:
    Code:
    $row = 10;
    
    while (running)
    { if (columnX==0) { break;}
      $row--;
    }
    
    echo ($row);
    $row will retain the $row value when the columnX==0.
    You use break when you don't want to continue to do anymore iteration (for/while)
    Hope this is what you need
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can achieve your purpose with much simpler code that doesn't hammer the database server with queries. SQL is your friend.

    Some notes:

    PHP Code:
    $result mysql_query("select * from philring");
    $number_of_sites=mysql_num_rows ($result); 
    Here you want to find out how many rows are in your table but you make the database server return the *entire* contents of the table. Not very efficient. Use something llike my code below.
    PHP Code:
    $sql "SELECT COUNT(*) AS rowCount FROM philring";
    $result mysql_query($sql);
    $row mysql_fetch_row($result);
    $number_of_sites $row[0]; 
    Even better would be to count on the primary key COUNT(primary_key) - but I don't know which column your primary key is.

    Next, as you can see in the code I wrote below, you don't need any while loops. You can construct an SQL query to achieve the same result. BTW - with the code you have (the while loop) you have a potential infinite loop!

    Here is the code I wrote up. Hope it is of help.
    PHP Code:
    <?php


    // connect to the database server
    $dbcnx = @mysql_connect("localhost""root""pass");
    if (!
    $dbcnx)
    {
        echo( 
    "<p>Unable to connect to the database server at this time.</p>");
        exit();
    }

    // select the philring database
    if (! @mysql_select_db("philring") )
    {
        echo( 
    "<p>Unable to conect to the database at this time</p>");
        exit();

    }

    //get the number of rows in the table (gets the numer of 
    //sites in the ring)

    $sql "SELECT COUNT(*) AS rowCount FROM philring";

    $result mysql_query($sql);
    $row mysql_fetch_row($result);
    $number_of_sites $row[0];

    if (@
    $direction == "next")
    {
        
    $id++;
        if (
    $id $number_of_sites)
        {
           
    $id=1;
        }
    }

    if (@
    $direction == "previous")
    {
        
    $id--;
        if(
    $id 1)
        {
           
    $id=$number_of_sites;
        }
    }

    //slect url and status from database

    $sql "SELECT url 
            FROM philring 
            WHERE id >= 
    $id
            AND status = 1
            ORDER BY id
            LIMIT 1"
    ;


    $result mysql_query($sql);

    if ( ! 
    $row mysql_fetch_array($result) )
    {
       
    // there were no records with an id >= $id and
       // a status = 1.
       // What are you going to do here? Reset $id to 1 and start the
       // search again?
       //eg:
       
       
    $id 1;
       
    $result mysql_query($sql);
       
    $row mysql_fetch_array($result)
    }

    echo(
    "<meta http-equiv=\"refresh\" content=\"0;URL=$row[0]\">");

    ?>
    Sorry about the double spacing - must be a problem due to copy-and-paste from BBEdit
    Last edited by freakysid; Mar 19, 2001 at 19:21.

  6. #6
    SitePoint Zealot p2ranger's Avatar
    Join Date
    Jan 2001
    Location
    Colorado, USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help. I never thought about going at it from that angle, using MySQL more correctly.

    It works fine going back and forth within the limits(first and last sites), but I'm still having problems getting it to work around the special situations. IE when it is at the last id an needs to go to the first or at the first and needs to go back to the last, it hangs up.

    thanks for the insight

    Jason
    ><>

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sid,

    As a small side note this code:

    PHP Code:
    $sql "SELECT COUNT(*) AS rowCount FROM philring";
    $result mysql_query($sql);
    $row mysql_fetch_row($result);
    $number_of_sites $row[0]; 
    Could be rewritten without using the overhead of the mysql_fetch_row() when all you need is one result from one column the COUNT(*) column using mysql_result

    PHP Code:
    $sql mysql_query("SELECT COUNT(*) AS rowCount FROM philring");
    $number_of_sites mysql_result($sql0); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - I've never thought of that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •