• I have written some clunky PHP code to emulate a spreadsheet which will work within WordPress. It has worked perfectly with [insert_php]. Sadly it does not as a snippet. Here is just part of the code in question, which displays the rows of an income spreadsheet in a table, with a couple of the columns containing drop-down selections. If I remove the ‘for ($m …)’ loop, then the code runs. However I can see nothing wrong with this loop – and I have even tried replacing it with a ‘while’ loop, but still no joy. I haven’t much hair left to tear out, so grateful for any insights.

    for ($i=0; $i <= $countIncome; $i++) {
    echo “<tr>”;
    for ($j = 0; $j < 15; $j++) {
    if ($j == 2) {
    echo “<td><select name=’ss[” . $i . “][” . $j . “]’>” ;
    for ($k=0; $k < $countIncTypes; $k++) {
    echo “<option value='” . $incTypes[$k][‘id’] . “‘”;
    if ( $income[$i][$j] == $incTypes[$k][‘id’] ) {
    echo ” selected “;
    }
    echo “>” . $incTypes[$k][‘aname’] . “</option>”;
    }
    echo “</select></td>”;
    } elseif ($j == 3) {
    echo “<td><select name=’ss[” . $i . “][” . $j . “]’>” ;
    echo “<option value = ”> -select- </option>”;
    for ($m=0; $m < $countTitles; $m++) { // does not like this line
    echo “<option value='” . $titles[$m][‘id’] . “‘”;
    if ( $income[$i][$j] == $titles[$m][‘id’] ) {
    echo ” selected “;
    }
    echo “>” . $titles[$m][‘title’] . “</option>”;
    }
    echo “</select></td>”;
    } else {
    echo “<td><input name=’ss[” . $i . “][” . $j . “]’ value = ‘” . $income[$i][$j] . “‘ type=’text’ “;
    // record id for this column not to be amended
    if ($j == 0) {
    echo “class=’rtb’ readonly “;
    // enable the date picker for this column
    } elseif ($j == 1) {
    echo “class=’rtb, custom_date’ “;
    } elseif (($j == 1) AND ($i == 0)) {
    echo ” readonly “;
    } else {
    echo “class=’rtb'”;
    }
    echo “/></td>”;
    }
    }
    echo “</tr>”;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author webcraftic

    (@webcraftic)

    Hi,

    I’ll help you check this code and fix it if necessary. Just paste the code with the formatting so that I see it in the original, and not after filtering the form.

    https://www.ads-software.com/support/topic/before-creating-a-new-topic-please-read/

    Best regards, Alex

    Thread Starter johnelk

    (@johnelk)

    Thanks Alex. Here it is (lengthy, I’m afraid, and of course there’s no data):

    //**********************************
    // for date picker
    //**********************************
    wp_enqueue_script(‘jquery-ui-datepicker’);
    wp_enqueue_style(‘jquery-style’, ‘https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css&#8217;);

    //*********************************************
    // check for user logged in with right privileges
    //*********************************************
    $loggedin = false;
    global $current_user;
    get_currentuserinfo();
    if(!$current_user->user_login == ” ) {
    $level = $current_user->user_level;
    if ($level > 5) {
    $loggedin = true;
    $sysmsg = “User can edit.”;
    } else {
    $sysmsg = “User cannot edit.”;
    }
    } else {
    $sysmsg = “User cannot edit.”;
    }

    // ************************
    // initialise
    // ************************
    $thisyear = date(‘Y’);
    $nextyear = date(‘Y’, strtotime(‘+ 1year’));
    $lastyear = date(‘Y’, strtotime(‘- 1year’));
    $prevyear = date(‘Y’, strtotime(‘- 2year’));

    $msg = “”;
    $titles = getgrouptitles();
    $countTitles = count($titles);
    $incTypes = getincometypes();
    $countIncTypes = count($incTypes);

    // ************************
    //process each selection
    // ***********************
    if(isset($_POST[‘loadss’])){
    setyear($_POST[‘ayear’]);
    }
    if(isset($_POST[‘showgrouppayments’])){
    $ayear = getyear();
    $grpPayments = showgrouppayments($ayear);
    $countGrpPayments = count($grpPayments);
    $msg .= $grpPayments[0][‘message’];
    }
    if(isset($_POST[‘createss’])){
    if ($loggedin) {
    createss($_POST[‘ayear’]);
    }
    }
    if(isset($_POST[‘save’])){
    if ($loggedin) {
    $ayear = getyear();
    $msg .= savespreadsheet($_POST, $ayear);
    }
    }
    if(isset($_POST[‘save2’])){
    if ($loggedin) {
    $ayear = getyear();
    $msg .= saveexpspreadsheet($_POST, $ayear);
    }
    }
    if(isset($_POST[‘del’])){
    if ($loggedin) {
    $ayear = getyear();
    $msg .= delincssrow($_POST[‘incrow’], $ayear);
    }
    }
    if(isset($_POST[‘del2’])){
    if ($loggedin) {
    $ayear = getyear();
    $msg .= delexpssrow($_POST[‘exprow’], $ayear);
    }
    }
    if(isset($_POST[‘printinc’])){
    if ($loggedin) {
    $ayear = getyear();
    $email = $_POST[‘post_email’];
    $msg .= incomespreadhtml($ayear, $email);
    }
    }
    if(isset($_POST[‘printexp’])){
    if ($loggedin) {
    $ayear = getyear();
    $email = $_POST[‘post_email’];
    $msg .= expensespreadhtml($ayear, $email);
    }
    }

    //*****************************************
    // Initialise
    // **************************************
    $ayear = getyear();
    $byear = $ayear+1;

    $income = getspreadsheet($ayear);
    $countIncome = count($income);
    $msg .= $income[0][15];

    $expenses = getexpspreadsheet($ayear);
    $countExpenses = count($expenses);
    $msg .= $expenses[0][17];

    //************************
    // calculate income totals
    //************************
    $tot6=0.0;$tot7=0.0;$tot8=0.0;$tot9=0.0;$tot10=0.0;$tot11=0.0;$tot12=0.0;$tot13=0.0;
    for ($i=0;$i < $countIncome; $i++) {
    $tot6+= $income[$i][6]; //bf
    $tot7+= $income[$i][7]; //subs
    $tot8+= $income[$i][8]; //mags
    $tot9+= $income[$i][9]; //meet
    $tot10+= $income[$i][10]; //xmas_lunch
    $tot11+= $income[$i][11]; //GA
    $tot12+= $income[$i][12]; //PP
    $tot13+= $income[$i][13]; //Misc
    }
    $grandtotalinc = number_format(($tot6+$tot7+$tot8+$tot9+$tot10+$tot11+$tot12+$tot13),2,’.’, ”);

    //************************
    // calculate expenditure totals
    //************************
    $tot14=0.0;$tot15=0.0;$tot16=0.0;$tot17=0.0;$tot18=0.0;$tot19=0.0;$tot20=0.0;$tot21=0.0;$tot22=0.0;$tot23=0.0;$tot24=0.0;
    for ($i=0;$i < $countExpenses; $i++) {
    $tot14+= $expenses[$i][5]; //room
    $tot15+= $expenses[$i][6]; //speak
    $tot16+= $expenses[$i][7]; //news
    $tot17+= $expenses[$i][8]; //admin
    $tot18+= $expenses[$i][9]; //fees
    $tot19+= $expenses[$i][10]; //equip
    $tot20+= $expenses[$i][11]; //xmas
    $tot21+= $expenses[$i][12]; //ins
    $tot22+= $expenses[$i][13]; //sw
    $tot23+= $expenses[$i][14]; //misc
    $tot24+= $expenses[$i][15]; //auditor
    }
    $grandtotalexp = number_format(($tot14+$tot15+$tot16+$tot17+$tot18+$tot19+$tot20+$tot21+$tot22+$tot23+$tot24),2,’.’, ”);

    //**************************************
    // Display instructions
    //*************************************
    echo “Add a row. Scroll down to the bottom of the spreadsheet in question, and enter in the data. <br />”;
    echo “Note 1: A new row will only be saved if a date has been entered.<br />”;
    echo “Note 2: You may only save one NEW row at a time. You can, though, save multiple changes to the existing spreadsheet.<br />”;
    echo “Save the spreadsheet Press Save for the spreadsheet in question. <br />”;
    echo “Delete a row. Enter the row (ID) number in the box next to the Delete button and press Delete.<br />”;
    echo “Show group payments. Press ‘Show Payments’ button to display the list of group payments to date. (List shown below).<br />”;
    echo “Create new spreadsheets. Select the year and press Create Spreadsheets<br /><br />”;
    echo “User access level: <span style = ‘color:red’> ” . $sysmsg . “</span><br />”;
    echo “System message: <span style = ‘color:red’> ” . $msg . “</span><br />”;

    //**************************************
    // Display choices
    //**************************************
    echo “<div class=’left’>”;
    echo “<form action=” method=’post’>”;
    echo “Select year: “;
    echo “<select name=’ayear’>”;
    echo “<option value='” . $thisyear . “‘”;
    if ($thisyear == $ayear) echo ” selected “;
    echo “>” . $thisyear . ” / ” . $nextyear . “</option>”;
    echo “<option value='” . $lastyear . “‘”;
    if ($lastyear == $ayear) echo ” selected “;
    echo “>” . $lastyear . ” / ” . $thisyear . “</option>”;
    echo “<option value='” . $prevyear . “‘”;
    if ($prevyear == $ayear) echo ” selected “;
    echo “>” . $prevyear . ” / ” . $lastyear . “</option>”;
    echo “</select>”;
    echo ” <input type=’submit’ name=’loadss’ value=’Load spreadsheet’/>”;
    echo ” <input type=’submit’ name=’createss’ value=’Create spreadsheets’/>”;
    echo ” <input type=’submit’ name=’showgrouppayments’ value=’Show payments’/><br /><br />”;
    echo “</form></div>”;

    //*****************************************
    // Display income table
    // ****************************************
    echo “<div class=’clr’></div>”;
    echo “<h3>Income</h3>”;
    echo “<form action=” method=’post’><table id=’spread’><thead>”;
    echo”<tr><th>ID</th><th>Date</th><th>Type</th><th>Group</th><th>Recon</th><th>Folio</th><th>BrForw</th><th>Subs</th>”;
    echo “<th>Mags </th><th>Meetings</th><th>Lunch</th><th>GA</th><th>PayPal</th><th>Misc</th><th>Desc</th></tr></thead><tbody>”;
    echo “<tr><td> ” . $ayear .” – ” . $byear . “</td><td></td><td> Balance:</td><td>“;
    echo ” ” . number_format(($grandtotalinc-$grandtotalexp),2,’.’, ”) .”
    </td><td> Totals:</td><td> ” . number_format($grandtotalinc,2) . “</td>”;
    echo “<td> ” . number_format($tot6,2) . “</td>”;
    echo “<td> ” . number_format($tot7,2) . “</td>”;
    echo “<td> ” . number_format($tot8,2) . “</td>”;
    echo “<td> ” . number_format($tot9,2) . “</td>”;
    echo “<td> ” . number_format($tot10,2) . “</td>”;
    echo “<td> ” . number_format($tot11,2) . “</td>”;
    echo “<td> ” . number_format($tot12,2) . “</td>”;
    echo “<td> ” . number_format($tot13,2) . “</td><td></td></tr>”;
    for ($i=0; $i <= $countIncome; $i++) {
    echo “<tr>”;
    for ($j = 0; $j < 15; $j++) {
    if ($j == 2) {
    echo “<td><select name=’ss[” . $i . “][” . $j . “]’>” ;
    for ($k=0; $k < $countIncTypes; $k++) {
    echo “<option value='” . $incTypes[$k][‘id’] . “‘”;
    if ( $income[$i][$j] == $incTypes[$k][‘id’] ) {
    echo ” selected “;
    }
    echo “>” . $incTypes[$k][‘aname’] . “</option>”;
    }
    echo “</select></td>”;
    } elseif ($j == 3) {
    echo “<td><select name=’ss[” . $i . “][” . $j . “]’>” ;
    echo “<option value = ”> -select- </option>”;
    for ($m=0; $m < $countTitles; $m++) {
    echo “<option value='” . $titles[$m][‘id’] . “‘”;
    if ( $income[$i][$j] == $titles[$m][‘id’] ) {
    echo ” selected “;
    }
    echo “>” . $titles[$m][‘title’] . “</option>”;
    }
    echo “</select></td>”;
    } else {
    echo “<td><input name=’ss[” . $i . “][” . $j . “]’ value = ‘” . $income[$i][$j] . “‘ type=’text’ “;
    // record id for this column not to be amended
    if ($j == 0) {
    echo “class=’rtb’ readonly “;
    // enable the date picker for this column
    } elseif ($j == 1) {
    echo “class=’rtb, custom_date’ “;
    } elseif (($j == 1) AND ($i == 0)) {
    echo ” readonly “;

    } else {
    echo “class=’rtb'”;
    }
    echo “/></td>”;
    }
    }
    echo “</tr>”;
    }
    echo “</tbody></table><br /><br />”;
    //****************************
    // Selections
    //*****************************
    echo “<input type=’submit’ name=’save’ value=’Save income’ onclick=’return confirm(\”Are you sure?\”)’ />”;
    echo ”  <input type = ‘submit’ name=’del’ value=’Delete Row:’ onclick=’return confirm(\”Are you sure?\”)’ /> Row ID Nr. <input type=’text’ size=’6′ name=’incrow’ /> “;
    echo “Email <select name=’post_email’>”;
    echo “<option name=’chair’ value=’[email protected]’>Send to Chair</option>”;
    echo “<option name=’webmanager’ value=’[email protected]’>Send to Web Manager</option>”;
    echo “<option name=’membership’ value=’[email protected]’>Send to Membership Secretary</option>”;
    echo “<option name=’newmembership’ value=’[email protected]’>Send to New Membership Secretary</option>”;
    echo “<option name=’technical’ value=’[email protected]’>Send to Technical Manager</option>”;
    echo “<option name=’treasurer’ value=’[email protected]’>Send to Treasurer</option>”;
    echo “</select> “;
    echo ”  <input type=’submit’ name=’printinc’ value=’Send file’ onclick=’return confirm(\”Are you sure?\”)’ />”;
    echo “</form>”;
    echo “<div class=’clr’></div>”;
    //***************************************************************
    // display expenditure table
    //***************************************************************
    echo “<h3>Expenditure</h3>”;
    echo “<form action=” method=’post’><table id=’spread2′><thead>”;
    echo “<tr><th>ID</th><th>Date</th><th>Payee</th><th>Recon</th><th>Folio</th><th>Rooms</th><th>Speakers</th>”;
    echo “<th>Newsletter </th><th>Admin</th><th>Fees</th><th>Equip</th><th>Lunch</th><th>Insurance</th><th>SW</th><th>Misc</th><th>Audit</th><th>Description</th></tr></thead><tbody>”;
    echo “<tr><td> ” . $ayear .” – ” . $byear .”</td><td></td><td></td><td> Totals:</td><td> ” . number_format($grandtotalexp,2) . “</td>”;
    echo “<td> ” . number_format($tot14,2) . “</td>”;
    echo “<td> ” . number_format($tot15,2) . “</td>”;
    echo “<td> ” . number_format($tot16,2) . “</td>”;
    echo “<td> ” . number_format($tot17,2) . “</td>”;
    echo “<td> ” . number_format($tot18,2) . “</td>”;
    echo “<td> ” . number_format($tot19,2) . “</td>”;
    echo “<td> ” . number_format($tot20,2) . “</td>”;
    echo “<td> ” . number_format($tot21,2) . “</td>”;
    echo “<td> ” . number_format($tot22,2) . “</td>”;
    echo “<td> ” . number_format($tot23,2) . “</td>”;
    echo “<td> ” . number_format($tot24,2) . “</td><td></td></tr>”;

    for ($i=0; $i <= $countExpenses; $i++) {
    echo “<tr>”;
    for ($j = 0; $j < 17; $j++) {
    echo “<td><input name=’ss[” . $i . “][” . $j . “]’ value = ‘” . $expenses[$i][$j] . “‘ type=’text’ “;
    // record id for this column not to be amended
    if (($j == 0)) {
    echo “class=’rtb’ readonly “;
    }
    // enable the date picker for this column
    elseif ($j == 1) {
    echo “class=’rtb, custom_date’ “;
    } else {
    echo “class=’rtb'”;
    }
    echo “/></td>”;
    }
    echo “</tr>”;
    }
    echo “</tbody></table><br /><br />”;
    //*********************
    // selections
    //*********************
    echo “<input type=’submit’ name=’save2′ value=’Save expenditure’ onclick=’return confirm(\”Are you sure?\”)’ /> “;
    echo ”  <input type = ‘submit’ name=’del2′ value=’Delete Row:’ onclick=’return confirm(\”Are you sure?\”)’ /> Row ID Nr. <input type=’text’ size=’6′ name=’exprow’ />”;
    echo “Email <select name=’post_email’>”;
    echo “<option name=’chair’ value=’[email protected]’>Send to Chair</option>”;
    echo “<option name=’webmanager’ value=’[email protected]’>Send to Web Manager</option>”;
    echo “<option name=’membership’ value=’[email protected]’>Send to Membership Secretary</option>”;
    echo “<option name=’newmembership’ value=’[email protected]’>Send to New Membership Secretary</option>”;
    echo “<option name=’technical’ value=’[email protected]’>Send to Technical Manager</option>”;
    echo “<option name=’treasurer’ value=’[email protected]’>Send to Treasurer</option>”;
    echo “</select> “;
    echo ”  <input type=’submit’ name=’printexp’ value=’Send file’ onclick=’return confirm(\”Are you sure?\”)’ />”;
    echo “</form>”;
    echo “<div class=’clr’></div>”;
    //***************************************************
    // display group payments
    //***************************************************
    echo “<form action=” method=’post’>”;
    echo “<fieldset>”;
    echo “<legend>Group Payments</legend>”;
    echo “<table><tbody><tr>”;
    echo “<tr><th>Group</th><th>Date</th><th>Total</th><th>Venue</th></tr>”;
    for ($i=0;$i < $countGrpPayments; $i++) {
    echo “<tr><td>” . $grpPayments[$i][‘title’] . “</td>”;
    echo “<td>” . $grpPayments[$i][‘date’] . “</td>”;
    echo “<td>” . $grpPayments[$i][‘meetings’] . “</td>”;
    echo “<td>” . $grpPayments[$i][‘name’] . “</td>”;
    echo “</tr>”;
    }
    echo”</tbody></table></fieldset></form>”;

    //***************************************************
    // Javascript for the date picker display
    //***************************************************
    echo “<script type=’text/javascript’>
    jQuery(document).ready(function($) {
    $(‘.custom_date’).datepicker({
    dateFormat : ‘yy-mm-dd’
    });
    });
    </script>”;
    //****************************************************
    // Javascript for the scroll functions for both tables
    //****************************************************
    echo “<script type=’text/javascript’>
    document.getElementById(‘spread’).addEventListener(‘scroll’,function(){
    var translate = ‘translate(0,’+this.scrollTop+’px)’;
    this.querySelector(‘thead’).style.transform = translate;
    });
    document.getElementById(‘spread2’).addEventListener(‘scroll’,function(){
    var translate = ‘translate(0,’+this.scrollTop+’px)’;
    this.querySelector(‘thead’).style.transform = translate;
    });
    </script>”;

    Plugin Author webcraftic

    (@webcraftic)

    You again laid out the code without formatting. Please read this topic to learn how to make code formatting for posting on the forum:
    https://www.ads-software.com/support/topic/before-creating-a-new-topic-please-read/

    You can also send me this code in php file by e-mail.
    Email: [email protected]

    The php file is more preferable. I will check it and send it in a reply letter.

    Best regards, Alex

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Code doesn’t run as a snippet’ is closed to new replies.