An odd web-programming challenge...

ddrueding

Fixture
Joined
Feb 4, 2002
Messages
19,728
Location
Horsens, Denmark
Here is what I need to create:

A simple web page with a list of items (30-50)

1. When you click on an item, it is removed from that page and added to another page.

2. When you click on an item on the second page it is removed from that page and added to the first.

3. Setup can be complicated (involve raw editing of a database or other file), but using the page must be that simple (anyone who can get online can see, understand, and use it)

What is the easiest method to set this up? Is a database of some sort required? Or can clicking the item somehow modify the pages directly?

Thoughts?
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
If you plan to have a large number of people access the page at any given time, I'd suggest a database for this. You could manage this through writing to a plain text file, but I'd be afraid that too many changes at any given time could cause a lock on the text file. The issue being if two different users selected an item at the same time.

You could have page "A" select all items marked for page "A" in the database and page "B" select all items marked for page "B".

Clicking on an item on page "A" will write an update to the database and switch the associated page from A to B. The reverse would be true for page "B". It would only list items marked for page "B", so by clicking on any of them, they will be switched to page "A".
 

ddrueding

Fixture
Joined
Feb 4, 2002
Messages
19,728
Location
Horsens, Denmark
Handruin said:
If you plan to have a large number of people access the page at any given time, I'd suggest a database for this. You could manage this through writing to a plain text file, but I'd be afraid that too many changes at any given time could cause a lock on the text file. The issue being if two different users selected an item at the same time.

You could have page "A" select all items marked for page "A" in the database and page "B" select all items marked for page "B".

Clicking on an item on page "A" will write an update to the database and switch the associated page from A to B. The reverse would be true for page "B". It would only list items marked for page "B", so by clicking on any of them, they will be switched to page "A".

This seems fine for our application, the total expected load is <2000 hits spread out randomly over a 6-10 month period. I think the likelyhood of someone clicking at the exact same time is fairly unlikely.

In the (slightly more likely) event that someone attemps to click on an item after it has been moved (user 1 hasn't refreshed since user 2 clicked the item); is there any way to do error-trapping using the text method?
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
Code:
CREATE TABLE switch_data (
  switch_id int(11) NOT NULL auto_increment,
  switch_name text NOT NULL,
  switch_value tinyint(1) default NULL,
  PRIMARY KEY  (switch_id)
) TYPE=MyISAM;

I've created a small database that keeps track of the data. Sine the object was to switch between two pages, I'm using a binary value in "switch_value". Page "A" = 0 and Page "B" = 1.

The part I'm not certain about is if you need two separate files to represent page "A" and page "B". What I mean is that the content of the page can by dynamically changed by using a variable. So that you have one actual file, and the page changes based on the URL.

Page A:

www.yourpage.com/switcher.php?p=a

Page B:

www.yourpage.com/switcher.php?p=b
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
ddrueding said:
Handruin said:
If you plan to have a large number of people access the page at any given time, I'd suggest a database for this. You could manage this through writing to a plain text file, but I'd be afraid that too many changes at any given time could cause a lock on the text file. The issue being if two different users selected an item at the same time.

You could have page "A" select all items marked for page "A" in the database and page "B" select all items marked for page "B".

Clicking on an item on page "A" will write an update to the database and switch the associated page from A to B. The reverse would be true for page "B". It would only list items marked for page "B", so by clicking on any of them, they will be switched to page "A".

This seems fine for our application, the total expected load is <2000 hits spread out randomly over a 6-10 month period. I think the likelyhood of someone clicking at the exact same time is fairly unlikely.

In the (slightly more likely) event that someone attemps to click on an item after it has been moved (user 1 hasn't refreshed since user 2 clicked the item); is there any way to do error-trapping using the text method?

If you prefer a flat file, I'd recommend storing the data in XML and have the script generate the user-visible HTML page so that the flat file doesn't require a "read" (open) and "close" every time a user wants to simply view the page.

In the event a user makes a change, the script should update the XML file and then invole the script to generate the HTML page accordingly to the change. I have limited experience in opening/writing to flat text files, so I won't be of much help in this area.

As far as error trapping, it is definitly possible to test a condition and inform the user nicely of the situation. I would need to understand better how you plan to use this for me to give you an example of error checking.
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
Rough job completed using MySQL & PHP.

http://projects.handruin.com/switcher/switcher.php

I've combined both pages into one; however they can be separated with little effort. This example is only to illustrate your request in action, with both pages on the same page for immediate feedback of the result. There isn't a ton of error checking in use and the code could be arranged more dynamically. (It’s late...) For example, if the DB was unavailable, you would see a non-friendly error message.

Let me know if this is close to what you need and if so, I'll send you my code. (or post it here for others to tear apart)
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
Reason...slight embarrassment at my lousy coding skills. It's fairly messy. If ddrueding were interested in using this, I would write it cleaner. I was trying a proof of concept here...

Code:
<?php
//Capture GET data

$g_c = $_GET["c"];
$g_cur = $_GET["cur"];


//Define Path to this include
$def_root_path = './';

include($def_root_path . 'functions.php');


//Changer

//Check current item before we process...
if($g_c && $g_cur){
	$sql_page_check = "SELECT `switch_value` FROM `switch_data` WHERE `switch_id` = $g_c";
	$db_container_check=db_connect($sql_page_check);
	$row_check = mysql_fetch_array($db_container_check);
}

switch ($g_cur) {
	case a:	
	if($row_check['switch_value'] == 0){
		$sql_page0_remove = "UPDATE switch_data SET `switch_value` = 1 WHERE switch_id = $g_c";
		$db_container_update=db_connect($sql_page0_remove);
		echo "
This worked correctly.  $g_c was switched from page A to B
";	
	}
	else
	{
	echo "[b]Item Previously updated[/b] from page A to page B, no change has been made. (page updated to reflect change)
";
	}
	break;

	case b:	
	if($row_check['switch_value'] == 1){
		$sql_page0_remove = "UPDATE switch_data SET `switch_value` = 0 WHERE switch_id = $g_c";
		$db_container_update=db_connect($sql_page0_remove);
		echo "
This worked correctly.  $g_c was switched from B to A
";	
	}
	else
	{
	echo "[b]Item Previously updated[/b] from page B to page A, no change has been made.  (page updated to reflect change)
";
	}
	break;

	default:
	echo "!DEBUG_INFO! No Change Made 
";
	break;
}



$sql_page0 = "SELECT `switch_id`, `switch_name`, `switch_value` FROM `switch_data` WHERE `switch_value` = 0 ORDER BY `switch_id` ASC";
$db_container_a=db_connect($sql_page0);
$num_rows = mysql_num_rows($db_container_a);

$template_a= "
Page A
<table border=1 cellpadding=1 cellspacing=0>
		<tr>
		<td>Switch ID</td><td>Switch Name</td><td>Group Value</td><td>Remove ?</td>
		</tr>";

$c_a=0;
		echo $template_a;
		while ($row = mysql_fetch_array($db_container_a)){

			$s_id=$row['switch_id'];
			$s_name=$row['switch_name'];
			$s_value=$row['switch_value'];

			echo "<tr bgcolor=".row_color($c_a).">
			<td>$s_id</td><td>$s_name</td><td>$s_value</td><td align=center><a href=\"switcher.php?c=$s_id&cur=a\">X</a></td>
			</tr>";
			$c_a++;
		}

	echo "</table>\n";







$sql_page1 = "SELECT `switch_id`, `switch_name`, `switch_value` FROM `switch_data` WHERE `switch_value` = 1 ORDER BY `switch_id` ASC";
$db_container_b=db_connect($sql_page1);
$num_rows_b = mysql_num_rows($db_container_b);

$template_b= "

Page B
<table border=1 cellpadding=1 cellspacing=0>
		<tr>
		<td>Switch ID</td><td>Switch Name</td><td>Group Value</td><td>Remove ?</td>
		</tr>";

$c_b=0;
		echo $template_b;
		while ($row_b = mysql_fetch_array($db_container_b)){

			$s_id_b=$row_b['switch_id'];
			$s_name_b=$row_b['switch_name'];
			$s_value_b=$row_b['switch_value'];

			echo "<tr bgcolor=".row_color($c_b).">
			<td>$s_id_b</td><td>$s_name_b</td><td>$s_value_b</td><td align=center><a href=\"switcher.php?c=$s_id_b&cur=b\">X</a></td>
			</tr>";
			$c_b++;
		}

	echo "</table>\n";
?>
 

ddrueding

Fixture
Joined
Feb 4, 2002
Messages
19,728
Location
Horsens, Denmark
Handy, you are handy.....;)

That works very well. Thank you!

The application is a simple gift registry for my sister's wedding. The problem with all the ones that are currently available are twofold:

1. They requre significant computer knowledge to function properly. For many the process of creating a login is too much.

2. They encourage e-commperce through specific sites, my sister wants to encourage everyone to buy the gifts from their own independant stores (like the true hippy she is).

I'd love to take this and use it, there's only a few things left to change.

1. Separate it into 2 pages
2. have the only text visible on each page be the item names themselves, and have clicking on the item name trigger the move.

This is great, I can't thank you enough.
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
ddrueding said:
Handy, you are handy.....;)

That works very well. Thank you!

The application is a simple gift registry for my sister's wedding. The problem with all the ones that are currently available are twofold:

1. They requre significant computer knowledge to function properly. For many the process of creating a login is too much.

2. They encourage e-commperce through specific sites, my sister wants to encourage everyone to buy the gifts from their own independant stores (like the true hippy she is).

I'd love to take this and use it, there's only a few things left to change.

1. Separate it into 2 pages
2. have the only text visible on each page be the item names themselves, and have clicking on the item name trigger the move.

This is great, I can't thank you enough.

I'll make this into two pages. I'll also change the item text into the remove link. Wouldn't that be more confusing than having the word "remove" next to the item? Making the item clickable might give the impression that more information is available.

Anyway, it's your choice and easy to change so I'll make it the way you suggested.
 

ddrueding

Fixture
Joined
Feb 4, 2002
Messages
19,728
Location
Horsens, Denmark
Thank you. I was a bit undecided at first, but I will include some HTML at the beginning of each page to describe what is happening in detail. I think keeping the page as simple as possible is the best way to go.

Thanks again.
 

Handruin

Administrator
Joined
Jan 13, 2002
Messages
13,920
Location
USA
I have a couple more updates coming. I've made a change so that the page informs the user if there are no more items available.
 
Top