Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 21st, 2004, 07:07 PM
Registered User
 
Join Date: Feb 2004
Location: Anaheim, CA, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default change order of an array automatically after add

Iíd like to reset the order automatically.

I have a set up like this:

items sort
----------------------------------
lamp 1
pen 2
XXX 3
disk 3
YYY 4

1. If I add a new item name [YYY] to the end of the table, then I am able to make YYY appeared as number 4 on the list.
2. If I add a new item name [XXX] to anywhere in the table, then I have trouble with the order. Now I have an array set up to sort the list by typing a desired number in each box. Itís ok for a short list of 10 or 20 items. When I have a long list of 50 or more items, Iíd like to sort them automatically after a new item got added.


Any ideas or suggestions would be greatly appreciated.

sau

Reply With Quote
  #2 (permalink)  
Old February 21st, 2004, 07:15 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Have a look at asort() or sort().

if (count($foo_array) >= 50)
{
    asort($foo_array);
}

The difference between asort() and sort() is sort() will sort the array and reassign indices (numeric) rather than simply reordering the keys whereas asort() will preseve key association, just rearrange the key => value location in the array.

Array functions:
http://www.php.net/array

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old February 21st, 2004, 07:17 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

BTW: these function sort by value, not by key, if you want to sort by key use ksort().

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old February 21st, 2004, 08:09 PM
Registered User
 
Join Date: Feb 2004
Location: Anaheim, CA, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just read those sort(), asort(), and ksort(). They are very useful but i don't think i can appy them to my case.


items sort
----------------------------------
lamp 1
pen 2
XXX 3
disk 3
YYY 4

I allow users to add a new item to the list and make the new item appear in an order that they wish.
As the above example, they want item XXX to be number 3 on the list. So i added XXX to the list and assigned value for [sort] as [sort] + 1. The trouble is that: i end up having 2 records with the [sort] value as 3. My wish is to include some code that can automatically shuffle the list so that the list will appear as:

lamp 1
pen 2
XXX 3
disk 4
YYY 5

Is it possible to do like that? or am i just asking silly question?

thank you very much

sau


Reply With Quote
  #5 (permalink)  
Old February 21st, 2004, 08:30 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Sorry, I suppose I misunderstood your problem.
I don't beleive in silly questions!††Sure it is possible.

First write your program to do the following:

1.) Before adding the new record check for existence of the record to be added. Or, in other words, write a boolean function like is_record() that will return true if the record already exists.

2.) If the record does already exist write an shuffle_records() function to shuffle the indices beginning with the record in question. Loop through each record starting with that record and apply an increment method... $i++.

3.) Then insert the new record.

Without more background information, this may not be the best method.††But definitely one that will do the trick.

Suppose the following...

$foo[1] = 'lamp';
$foo[2] = 'pen';
$foo[3] = 'disk';
$foo[4] = 'yyy';

function is_record($needle, $haystack)
{
    return array_key_exists($needle, $haystack);
}

function shuffle_records($key, $records)
{
    // Assuming you're always going to have the records numbered
    // without any gaps, this will work

    for ($key; $key < count($records); $key++)
    {
    †††$new_records[$key + 1] = $records[$key];
    }

    return $new_records;
}

if (is_record(3, $foo))
{
    $foo = shuffle_records(3, $foo);
    $foo[3] = 'XXX';
}
else
{
    $foo[3] = 'XXX';
}

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old February 21st, 2004, 10:23 PM
Registered User
 
Join Date: Feb 2004
Location: Anaheim, CA, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think we are almost there. You're right. I didn't mention about using array. I just know i will have to use it. I am not good with array at all.

I am trying to provide to you more background. Hopfully i don't confuse you much.
--------------------------------------
1. Adding
on an add form, i have a querystring like below:
<a href="add.php?sort=$sort">Add a new item</a>
for example $sort = 2 at this time.
When the form gets submitted, the below will do the adding job.
$sort = $sort + 1;
$add = mssql_query("insert into myTable ([items], [sort]) values ('$items', '$sort')", $linkid);
-------------------------------------------------
2. Sorting
After adding, i display a form to show all records as below

items sort
-------------
lamp 1
pen 2
XXX 3
disk 3
YYY 4
-------------
I do have unique id for each record.
On this form, right now i have it set up as below:
<form....>
....
<input name="arrange[<? print $id; ?>]" type=text value="<? print $sort; ?>" size="7">
<input type=hidden name="id" value="<? print $id; ?>">
</form>
When the form gets sent for sorting, the below code will do the sorting job.

foreach($_POST['arrange'] as $id => $sort)
{
$query = mssql_query("update myTable set [sort] = '$sort' where [id] = '$id'", $linkid);
}
---------------------------------------------------------------
It is working just fine by manually typing a number in the text box for [sort] field and send it.

Now i'm thinking to count all the records. In the above example i have 4 records in total. I'd like to do a for loop some how that I can change the value of the sort field based on the total of all records. It will loop through and give first row value of 1, then 2 and so till 4.

$query = mssql_query("select [location], COUNT(sort) from myTable
where [location]='$location'", $linkid);
while($data=mssql_fetch_array($query))
{ $lastRecord = $data[count(sort)];
  }//end loop
for($count = 1; $count <=$lastRecord; $count++) //right here
{
  foreach($_POST['arrange'] as $id => $count)
    {
$query = mssql_query("update myTable set [sort] = '$count' where [id] = '$id'", $linkid);
     }

}

So that is what i have been trying to do. The new way didn't work for me.

thank you Rich so much.

Reply With Quote
  #7 (permalink)  
Old February 22nd, 2004, 12:27 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Well I'm not sure how different the MSSQL syntax is going to be.. but I can do an example with what would be possible in MySQL.. I'll let you figure out if the syntax is valid in MSSQL.

First check to see if the record exists

Code:
if (1 == mssql_num_rows(mssql_query("SELECT * FROM myTable WHERE [id] = '$id' AND [sort] = $sort", $linkid))
{
    // This index already exists, update the other results!
    // This query ought to push the sort field to make room for your insert

    if (!mssql_query("UPDATE myTable SET [sort] = [sort] + 1 WHERE [id] = '$id' AND [sort] >= $sort", $linkid))
    {
        echo 'Update failed!  MSSQL said: '; // Do your MSSQL error handling here.
    }
    else
    {
        // Now insert the record.
    }
}
else
{
    // Insert the record
}
That ought to work. Of course I haven't tested it. Seems to make sense to me.

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #8 (permalink)  
Old February 22nd, 2004, 12:51 AM
Registered User
 
Join Date: Feb 2004
Location: Anaheim, CA, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, you are so nice and dedicated to the forum.

It looks good to me. I'll give it a try. Now, i give myself a break.

Good night.

sau

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Add Primery Key value Automatically kamran Yousaf Bk C# 1 October 6th, 2007 05:39 PM
Add lines in php page automatically saifi4u PHP How-To 6 December 22nd, 2006 12:13 AM
Add lines in php page automatically saifi4u PHP How-To 1 December 19th, 2006 04:42 AM
Automatically change value abhisheksud Classic ASP Databases 0 December 13th, 2005 02:33 AM
add row no automatically Jane SQL Language 9 June 28th, 2004 03:11 AM



All times are GMT -4. The time now is 02:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.