Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    how can I write out a loop inside this array?

    Code:
    //this varible contains the array of existing users
    $existing_users=array('roshan','mike','jason');
    //value got from the get metho
    $user_name=$_POST['user_name'];
    //checking weather user exists or not in $existing_users array
    if (in_array($user_name, $existing_users))
    {
     //user name is not available
     echo "no";
    }
    else
    {
      //username available i.e. user name doesn't exists in array
      echo "yes";
    }
    the array near the top has a list of users, I would like to loop through my users in my database
    I know how to write a loop but not into here

    Code:
    $existing_users=array('roshan','mike','jason');

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    So you want to pre-load all your users into an array so the process of checking to see if the username is already used will be faster than if you select from your database every time a username has to be checked?

    This can be handled using normal mysql_fetch_array() looping syntax. Most people use WHILE ($row = mysql_fetch_array($result) syntax but I prefer a "for" loop.

    PHP Code:
    for ($i 0$i mysql_num_rows($result); $i++) {
        
    $row mysql_fetch_array($result);
        
    $username[$i] = $row['username'];


  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You wouldn't do this at all in regards to a database. The in_array check has unnecessary overhead as you'd need to draw all records in, put them into an array, and then check if the item is within the array. Unless you are planning on reusing this information repeatedly, this is way too much work.
    When using a database you should try to limit the amount of work overall by providing it a query for the user_name in a where condition. Then a simple mysql_num_rows (or whatever resultset counter is used for your database / library type is) to see if its <= 0. If it is, the user_name is not in use, otherwise its in use.

    The problem with the in_array check is simple:
    PHP Code:
    $aUsers = array();
    while (
    $row mysql_fetch_assoc($somequery))
    {
        
    $aUsers[] = $row['user_name'];
    }

    if (
    in_array($somecheck$aUsers))
    {
        echo 
    'already exists.';

    If you have 5000 users, a worst case scenario is 10,000 total theoretical loops, 5000 for the while draw, and 5000 for the in_array (although its not a real array so its not actually linear). If you were to not modify the query itself, a slightly better approach would be to check within the while for the query fetch:
    PHP Code:
    $bExists false;
    while ((
    $row mysql_fetch_assoc($qry)) && !$bExists)
    {
        if (
    $row['user_name'] = $toCheck)
        {
            
    $bExists true;
        }
    }

    if (
    $bExists)
    {
        echo 
    'Already in database.';

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    thanks, but I'm not sure where to use the loop in my code?!

  • #5
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    Can someone please help?!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Which approach are you planning on taking? A simple query without reusing any data, a list of all records to look up against, or an iterative check against records?
    If you don't need to keep the data at all, use a lookup. This won't require a loop at all, it would be a simple query with a mysql_num_rows check.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question

    yes the look up approach would be ok

  • #8
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    please help

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The lookup approach is the easiest, but I can't write the query for you. I can only write a query that represents what you may have.
    I'll assume you have an open database connection, which I'll represent as an include. This can be whatever connection you have embedded directly in this script, that doesn't really matter.
    PHP Code:
    <?php

    include_once 'dbconnect.php';

    if (isset(
    $_POST['user_name']))
    {
        if (
    function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
        {
            
    $_POST['user_name'] = stripslashes($_POST['user_name']);
        }

        
    // You can do any additional validation here
        
    $user_name mysql_real_escape_string($_POST['user_name']);

        
    $sQry 'SELECT user_name FROM userTable WHERE user_name = "' $user_name '"';
        
    $bInUse true;
        if (
    $qry = @mysql_query($sQry))
        {
            if (
    mysql_num_rows($qry) <= 0)
            {
                
    $bInUse false;
            }
        }
        else
        {
            echo 
    'Query has failed: ' mysql_error();
        }

        if (
    $bInUse)
        {
            echo 
    'That name is already in use.';
        }
        else
        {
            echo 
    'That name is available.';
        }
    }
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    jarv (12-11-2010)

  • #10
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Thumbs up

    YAY!!!! Thanks!!! xx


  •  

    Posting Permissions

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