Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have two snippets that work with different data in the script. What this does is I have 2 link sections. 1 section is just normal links and the other 1 is a referal section. Both of this sections can be added to, edited or removed when logged into the admin panel.

The feature that is NOT working on one of them is the sorter. Since you can create as many links or referers as you want, it prints out all the current ones in a form consisting of radio boxes from 1- (total number of items in this area) so you can chose which order they'll be displayed in on the site.

Ie.

ID | radio buttons to select order --------------------------- link1 1(*) 2() 3() 4() link2 1() 2(*) 3() 4() link3 1() 2() 3(*) 4() link4 1() 2() 3() 4(*)
From here, the admin can change the order in how this displays on the site any way they want.

This code was developed by a programmer a number of years ago and I can't figure it out. Can someone take a look at the 2 snippets below and tell me why the first one DOES work for the links but the one for the referrers prints out

ID | radio buttons to select order --------------------------- link1 1() 1() 1() 1(*) link2 1() 1() 1() 1(*) link3 1() 1() 1() 1(*) link4 1() 1() 1() 1(*)
Here's the LINKS section that prints out the form correctly
print "<center><table border='1'>\n"; print "<tr><td colspan=\"2\"><center><b>Adjust link di +splay order</b></center></td></tr>\n"; print "<form action='process.cgi' method='post'>\n"; print "<input type='hidden' name='section' value='links'>\n"; print "<input type='hidden' name='action' value='link_order'>\ +n"; $sql = "SELECT * FROM $links_table ORDER BY link_order ASC"; $sth = $dbh->prepare($sql); $sth->execute; $orders_list = join(":", @orders); $id_list = join(":", @ids); print "<input type='hidden' name='orders_list' value='$orders_ +list'>\n"; print "<input type='hidden' name='link_id_list' value='$id_lis +t'>\n"; while ($i = $sth->fetchrow_hashref) { my $id = $$i{"id"}; my $name = $$i{"name"}; my $order = $$i{"link_order"}; print "<tr><td>$name</td>\n"; print "<td>\n"; foreach $t_order (@orders) { print "$t_order <input type='radio' name='order_" . $i +d . "' value='$t_order'"; if ($order eq $t_order) { print " checked" } print ">"; } print "</td></tr>\n"; } print "<tr><td colspan=\"2\"><center><input type='subm +it'></center>\n"; print "</table></center>\n"; print "</form>\n";
Here's the code for the referer section that prints out all 1s instead of the correct sortable form.
print "<b><i>Adjust referrers display order</i></b><br>\n"; print "<table border='1'>\n"; print "<form action='process.cgi' method='post'>\n"; print "<input type='hidden' name='section' value='referrers'>\ +n"; print "<input type='hidden' name='action' value='link_order'>\ +n"; $sql = "SELECT * FROM $referrers_table ORDER BY link_order ASC +"; $sth = $dbh->prepare($sql); $sth->execute; $orders_list = join(":", @orders); $id_list = join(":", @ids); print "<input type='hidden' name='orders_list' value='$orders_ +list'>\n"; print "<input type='hidden' name='link_id_list' value='$id_lis +t'>\n"; while ($i = $sth->fetchrow_hashref) { my $id = $$i{"id"}; my $name = $$i{"name"}; my $order = $$i{"link_order"}; print "<tr><td>$name</td>\n"; print "<td>\n"; foreach $t_order (@orders) { print "$t_order <input type='radio' name='order_" . $i +d . "' value='$t_order'"; if ($order eq $t_order) { print " checked" } print ">"; } print "</td></tr>\n"; } print "</table>\n"; print "<input type='submit'>\n"; print "</form></center>\n"; }

READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: dynamic radio sorting
by Anonymous Monk on Jun 29, 2005 at 19:05 UTC
    I thought maybe it would be helpful to display the HTML dump of each of these, just in case.

    This is the HTML for the working LINKS section

    <center><table border='1'> <tr><td colspan="2"><center><b>Adjust link display order</b></center>< +/td></tr> <form action='process.cgi' method='post'> <input type='hidden' name='section' value='links'> <input type='hidden' name='action' value='link_order'> <input type='hidden' name='orders_list' value='1:2'> <input type='hidden' name='link_id_list' value='20:21'> <tr><td>FreeStuff</td> <td> 1 <input type='radio' name='order_20' value='1' checked>2 <input type= +'radio' name='order_20' value='2'></td></tr> <tr><td>Freebies</td> <td> 1 <input type='radio' name='order_21' value='1'>2 <input type='radio' +name='order_21' value='2' checked></td></tr> <tr><td colspan="2"><center><input type='submit'></center> </table></center> </form>
    And this is the HTML output of the broken referal section
    <i>Adjust referrers display order</i></b><br> <table border='1'> <form action='process.cgi' method='post'> <input type='hidden' name='section' value='referrers'> <input type='hidden' name='action' value='link_order'> <input type='hidden' name='orders_list' value='1:1:1:1:1:1'> <input type='hidden' name='link_id_list' value='21:14:16:22:20:19'> <tr><td>Site A</td> <td> 1 <input type='radio' name='order_21' value='1' checked>1 <input type= +'radio' name='order_21' value='1' checked>1 <input type='radio' name= +'order_21' value='1' checked>1 <input type='radio' name='order_21' va +lue='1' checked>1 <input type='radio' name='order_21' value='1' check +ed>1 <input type='radio' name='order_21' value='1' checked></td></tr> <tr><td>SiteB</td> <td> 1 <input type='radio' name='order_14' value='1' checked>1 <input type= +'radio' name='order_14' value='1' checked>1 <input type='radio' name= +'order_14' value='1' checked>1 <input type='radio' name='order_14' va +lue='1' checked>1 <input type='radio' name='order_14' value='1' check +ed>1 <input type='radio' name='order_14' value='1' checked></td></tr> <tr><td>Site C</td> <td> 1 <input type='radio' name='order_16' value='1' checked>1 <input type= +'radio' name='order_16' value='1' checked>1 <input type='radio' name= +'order_16' value='1' checked>1 <input type='radio' name='order_16' va +lue='1' checked>1 <input type='radio' name='order_16' value='1' check +ed>1 <input type='radio' name='order_16' value='1' checked></td></tr> <tr><td>Site D</td> <td> 1 <input type='radio' name='order_22' value='1' checked>1 <input type= +'radio' name='order_22' value='1' checked>1 <input type='radio' name= +'order_22' value='1' checked>1 <input type='radio' name='order_22' va +lue='1' checked>1 <input type='radio' name='order_22' value='1' check +ed>1 <input type='radio' name='order_22' value='1' checked></td></tr> <tr><td>Site E</td> <td> 1 <input type='radio' name='order_20' value='1' checked>1 <input type= +'radio' name='order_20' value='1' checked>1 <input type='radio' name= +'order_20' value='1' checked>1 <input type='radio' name='order_20' va +lue='1' checked>1 <input type='radio' name='order_20' value='1' check +ed>1 <input type='radio' name='order_20' value='1' checked></td></tr> <tr><td>Site F</td> <td> 1 <input type='radio' name='order_19' value='1' checked>1 <input type= +'radio' name='order_19' value='1' checked>1 <input type='radio' name= +'order_19' value='1' checked>1 <input type='radio' name='order_19' va +lue='1' checked>1 <input type='radio' name='order_19' value='1' check +ed>1 <input type='radio' name='order_19' value='1' checked></td></tr> </table> <input type='submit'> </form></center>
      Please check the values in @orders. The working code contains 1 2 3 4 5 and the other part is 1 1 1 1 1