Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2004, 07:07 PM
Registered User
 
Join Date: Feb 2004
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

 
Old February 21st, 2004, 07:15 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
:::::::::::::::::::::::::::::::::
 
Old February 21st, 2004, 07:17 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
:::::::::::::::::::::::::::::::::
 
Old February 21st, 2004, 08:09 PM
Registered User
 
Join Date: Feb 2004
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


 
Old February 21st, 2004, 08:30 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
:::::::::::::::::::::::::::::::::
 
Old February 21st, 2004, 10:23 PM
Registered User
 
Join Date: Feb 2004
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.

 
Old February 22nd, 2004, 12:27 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 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
:::::::::::::::::::::::::::::::::
 
Old February 22nd, 2004, 12:51 AM
Registered User
 
Join Date: Feb 2004
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






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Add Primery Key value Automatically kamran Yousaf Bk C# 1 October 6th, 2007 04: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 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.