...

View Full Version : how can I write out a loop inside this array?



jarv
12-08-2010, 07:11 PM
//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



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

Fumigator
12-08-2010, 07:19 PM
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.



for ($i = 0; $i < mysql_num_rows($result); $i++) {
$row = mysql_fetch_array($result);
$username[$i] = $row['username'];
}

Fou-Lu
12-08-2010, 07:22 PM
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:


$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:


$bExists = false;
while (($row = mysql_fetch_assoc($qry)) && !$bExists)
{
if ($row['user_name'] = $toCheck)
{
$bExists = true;
}
}

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

jarv
12-09-2010, 01:49 PM
thanks, but I'm not sure where to use the loop in my code?!

jarv
12-10-2010, 02:07 PM
Can someone please help?!

Fou-Lu
12-10-2010, 03:56 PM
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.

jarv
12-10-2010, 10:13 PM
yes the look up approach would be ok

jarv
12-11-2010, 02:17 PM
please help

Fou-Lu
12-11-2010, 06:31 PM
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

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.';
}
}

jarv
12-11-2010, 08:51 PM
YAY!!!! Thanks!!! xx



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum