Jump to content

Moving items in a table


Diegar
 Share

Recommended Posts

I apologize for having to dump a large piece of code, but I don't know how to explain it well otherwise.Here is a small snapshot of what i am trying to achieve. The table has four names on it. Each name is listed 1 - 4, and has 3 buttons (Up, Down and Remove), except for number 1, which doesn't have an UP, and number 4, which can't go down. The goal is to hit Up, Down, or Remove and have the appropriate action take place. If, for example, number 1 is removed, 2, 3 and 4 will slide up and 4 will say <empty> as the IF statement in that line tells it to.The real thing will have up to 20 names, so I was hoping there was a rather streamlined way to do this.

<?PHPrequire("dbconfig.php");$getTopQuery = "SELECT * FROM users WHERE user = '$user'";$getTopResults = mysql_query($getTopQuery,$msa) or DIE (mysql_error());$getTopArray = mysql_fetch_array($getTopResults) or Die (mysql_error());$name1 = $getTopArray["name1"];$name2 = $getTopArray["name2"];$name3 = $getTopArray["name3"];$name4 = $getTopArray["name4"];?><form name="TopNames" action="thisPage.php" method="POST"><table width="400" border="0" cellspacing="0" cellpadding="3">  <tr> 	<td bgcolor="#000033" align="center" colspan="10"> <b><font face="Arial, Helvetica, sans-serif" size="4" color="#FFFFFF">Top 	  10</font></b></td>  </tr>  <tr bgcolor="#FFFFFF"> 	<td width="20" align="center"><b><font color="#FF0000" face="Arial, Helvetica, sans-serif">1</font></b></td>	<td> 	  <?PHP if($name1 == "") {echo "<empty>";} else {echo $name1;}?>	</td>	<td width="20"> </td>	<td width="20"> 	  <input type="image" name="name1d" src="images/arrowDown.gif" value="Down"></td>	<td width="20"> 	  <input type="image" name="name1r" src="images/buttonRem.gif" value="Remove"></td>	<td width="20" align="center"><b><font color="#FF0000" face="Arial, Helvetica, sans-serif">3</font></b></td>	<td> 	  <?PHP if($name3 == "") {echo "<empty>";} else {echo $name3;}?>	</td>	<td width="20"> 	  <input type="image" name="name3u" src="images/arrowUp.gif" value="Up"></td>	<td width="20"> 	  <input type="image" name="name3d" src="images/arrowDown.gif" value="Down"></td>	<td width="20"> 	  <input type="image" name="name3r" src="images/buttonRem.gif" value="Remove"></td>  </tr>  <tr bgcolor="#FFFFFF"> 	<td width="20" align="center"><b><font color="#FF0000" face="Arial, Helvetica, sans-serif">2</font></b></td>	<td> 	  <?PHP if($name2 == "") {echo "<empty>";} else {echo $name2;}?>	</td>	<td width="20"> 	  <input type="image" name="name2u" src="images/arrowUp.gif" value="Up"></td>	<td width="20"> 	  <input type="image" name="name2d" src="images/arrowDown.gif" value="Down"></td>	<td width="20"> 	  <input type="image" name="name2r" src="images/buttonRem.gif" value="Remove"></td>	<td width="20" align="center"><b><font color="#FF0000" face="Arial, Helvetica, sans-serif">4</font></b></td>	<td> 	  <?PHP if($name4 == "") {echo "<empty>";} else {echo $name4;}?>	</td>	<td width="20"> 	  <input type="image" name="name4u" src="images/arrowUp.gif" value="Up"></td>	<td width="20"> </td>	<td width="20"> 	  <input type="image" name="name4r" src="images/buttonRem.gif" value="Remove"></td>  </tr></table></form>

I am not asking anyone to write this for me, but rather, to understand what i am trying to accomplish and offer guidance, and perhaps a few examples that I can feed off of.Thanks in advance!

Link to comment
Share on other sites

You'll want to store everything to print in an array, and you can loop through the array to show each item. In the loop to print things you can check if you are on the first or last item in the array and show the appropriate buttons. If you haven't worked with arrays before, check the array reference page:http://www.php.net/manual/en/language.types.array.php

Link to comment
Share on other sites

Okay... i now have the table writing itself in a loop, from an array of data.The buttons are now the issue for me, as they were to begin with as well. I can add the buttons to the dynamic rows, but i don't know how to make them individual, since the rows are created dynamically.I could make each button on each row have separate numeric value, but then each numbered value for each button would have to be defined. Seems wasteful to me, and isn't dynamic. Any suggestions?

Link to comment
Share on other sites

I don't understand what the problem is. This should be the body of your loop:

for ($i = 0; $i < count($array); $i++){	echo '<td width="20" align="center"><b><font color="#FF0000" face="Arial, Helvetica, sans-serif">' . $i . '</font></b></td>	<td>';	if($name2 == "") {echo ">empty>";} else {echo $name2;}	echo '</td>	<td width="20">';	if ($i != 0)	  echo '<input type="image" name="name' . $i . 'u" src="images/arrowUp.gif" value="Up">';	echo '</td>';	echo '<td width="20">'; 	if ($i != count($array) - 1)	  echo '<input type="image" name="name' .$i . 'd" src="images/arrowDown.gif" value="Down">';	echo '</td>';	echo '<td width="20"> 	  <input type="image" name="name' . $i . 'r" src="images/buttonRem.gif" value="Remove"></td>';}

Link to comment
Share on other sites

Maybe i just explained myself poorly. I have the table made. That isn't the issue. And it is a form, so if someone clicks the Up arrow on Line 2, it will send '2up' as a POST. My issue is how to process that information. Will i need to create a $variable1 = @$_POST["0up"], $variable2 = @$_POST["0down"], $variable3 = @$_POST["0rem"] ? Even if i put them into an array, i will have to create something like that for every single line. If i do that, then the table can't be expanded, without adding more lines. There has to be an easier way around this.

Link to comment
Share on other sites

Everything has a number. If your form contains an input that has the number of rows, then the processing script can read that number and loop through to read every row. So the loop would look something like this:

for ($i = 0; $i < intval($_POST['num_rows']); $i++){  $up = $_POST[$i . 'up'];  $down = $_POST[$i . 'down'];  $rem = $_POST[$i . 'rem'];  //do whatever you want with those values before looping again}

Link to comment
Share on other sites

Right, and thank you for that. That will help me with other things as well, but i have a problem with the way my my table is created. The table has basically 2 columns. If there are a total of 10 lines, then row 1 is 1 and 6, row 2 is 2 and 7. Does that make sense?

Link to comment
Share on other sites

I hate to say it, but this is where my newbie-ness comes into play. I don't know where to incorporate that, or what to do with it. I am sorry, i am new, but i am learning. I had modified my original WHILE statement, which was pretty static, to the one you gave me, plus a few modifications, to make it fit:

for ($r = 0; ($r < count($getTopArray) && $r <= 4); $r++)

But as for the newer example you left, i am clueless where that would go, or even what to do with it. The down button on each line, for instance, is something like this:

<?PHP $b = $r + 1; if (($getTopArray[$r] == "") ||  $getTopArray[$b] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'down" src="images/arrowDown.gif">'; } ?>

I don't have a clue what to do with the 'name0down' or whatever line they click, once they click it, or even really how to get it.

Link to comment
Share on other sites

<?PHPrequire("dbconfig.php");$user=$_COOKIE['user']; //Get the cookie$getTopQuery = "SELECT * FROM users WHERE user = '$user'";$getTopResults = mysql_query($getTopQuery,$msa) or DIE (mysql_error());$getArray = mysql_fetch_assoc($getTopResults) or Die (mysql_error());$getTopArray = array($getArray["name1"], $getArray["name2"] , $getArray["name3"] , $getArray["name4"] , $getArray["name5"] , $getArray["name6"] , $getArray["name7"] , $getArray["name8"] , $getArray["name9"] , $getArray["name10"]);?><html><body bgcolor="#FFFFFF" text="#000033"><form name="topTen" action="profile.php" method="POST"><table width="620" border="0" cellspacing="0" cellpadding="3">	<tr>		<td colspan="10" bgcolor="000033" align="center">			<font color="FFFFFF"><b>Top 10 names</b></font>		</td>	</tr><?PHP$bglight = "F1F1F1";$bgdark = "EBEBEB";$row = 0;for ($r = 0; ($r < count($getTopArray) && $r <= 4); $r++)	{	if($row == 2) $row = 0;	$bgcolor = $bglight; 	if($row == 1) {$bgcolor = $bgdark;}?>	<td width="50" align="center" bgcolor="<?PHP echo $bgcolor; ?>">		<font color="FF0000" size="4"><b><?PHP echo $r + 1; ?></b></font>	</td>	<td width="200" bgcolor="<?PHP echo $bgcolor; ?>">		<font><?PHP if ($getTopArray[$r] == "") { print "<empty>"; } else { print $getTopArray[$r]; } ?></font>	</td>	<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP if (($r == 0) || $getTopArray[$r] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'up" src="images/arrowUp.gif">'; } ?>	</td>	<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP $b = $r + 1; if (($getTopArray[$r] == "") ||  $getTopArray[$b] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'down" src="images/arrowDown.gif">'; } ?>	</td>	<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP if ($getTopArray[$r] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'rem" src="images/buttonRem.gif">'; } ?>	</td><?PHP$row++;		if($row == 2) $row = 0;	$bgcolor = $bglight; 	if($row == 1) {$bgcolor = $bgdark;}?>	<td width="50" align="center" bgcolor="<?PHP echo $bgcolor; ?>">		<font color="FF0000" size="4"><b><?PHP echo $r + 6; ?></b></font>	</td>	<td width="200" bgcolor="<?PHP echo $bgcolor; ?>">		<font><?PHP $r = $r + 5; if ($getTopArray[$r] == "") { print "<empty>"; } else { print $getTopArray[$r]; } ?></font>	</td>		<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP if (($r == 0) || $getTopArray[$r] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'up" src="images/arrowUp.gif">'; }  ?>	</td>	<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP $b = $r + 1; if ((($r >= 9) || $getTopArray[$r] == "") ||  $getTopArray[$b] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'down" src="images/arrowDown.gif">'; } ?>	</td>	<td width="17" bgcolor="<?PHP echo $bgcolor; ?>">		<?PHP if ($getTopArray[$r] == "") {echo ' ';} else { echo '<input type="image" name="name' . $r .'rem" src="images/buttonRem.gif">'; } $r = $r - 5; ?>	</td>	</tr>	<?PHP			}?></table></form></body></html>

Hope it isn't too bad looking...

Link to comment
Share on other sites

bit of house keeping:

$getTopArray = array($getArray["name1"], $getArray["name2"] , $getArray["name3"] , $getArray["name4"] , $getArray["name5"] , $getArray["name6"] , $getArray["name7"] , $getArray["name8"] , $getArray["name9"] , $getArray["name10"]);...for($r =...)...

Could be very easily changed to

//get rid of the $getTopArray variable and replace it with just things like this:foreach($getArray as $value){  //Continue your HTML here.}

And from what i understood of your problem, you could just do a simple check of the POST data. Let's say i click the "up" button on the row that has the ID index of 0, so the name in the post would 0up, right? Well then what you do is you take its name(0up) and you split that into 2 things using preg_split. After that, i would personally make 1 query to the database, getting the row's current order. Now, you do a check to see if the forum is going up/down. If up, you take the value retrieved from the database and add 1(I'm assuming they only move 1 space at a time), and if its going down, you subtract one. You then make another query, updating the row with the given ID of 0 to set its order to the newly achieved variable. Afterwards you have to check for a row which has the order of the new order created, but is not the row we just updated, and depending on if its an up or down switch, either subtract one or add one to the order, updating that row's order and voila...I could possible whip something up real quick if you need.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...