Well,

I think I've accomplished completely confusing myself.  I'll include
both the client-side and server-side scripts I'm using here. It's
pretty self-explanatory what I'm trying to accomplish, but I'm having
a great ordeal of trouble getting there.

Without further a due....:

dbsync.php
--
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd";>
<html lang="en">
<head>
<title>Database sync utility</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
<!-- Begin javascript functionality for checking/unchecking all
buttons in a form
function checkAs(obj_Button, s_Field, b_Checked)
{
        for(i_Element = 0; i_Element < obj_Button.form.elements.length; 
++i_Element)
        {
                if(obj_Button.form.elements[i_Element].name == s_Field)
                {
                        obj_Button.form.elements[i_Element].checked = b_Checked;
                }
        }
}

// Begin DOM ready Ajax here.
$(document).ready(function(){
    $.ajax({
                  url: "/dbserver.php",
                  type: "POST",
                  data: "db=alpha",
                  cache: false,
                  success: function(html){
                    $("#alpha_notes").append(html);
                  }
        });
        
        $.ajax({
                  url: "/dbserver.php",
                  type: "POST",
                  data: "db=brocas",
                  cache: false,
                  success: function(html){
                    $("#brocas_notes").append(html);
                  }
        });
});
        
function removeNote(id,name) {
        if(confirm("Are you sure you wish to delete: "+name+"?")) {
                // ajax code here to remove note.
                $.ajax({
                  url: "/dbserver.php",
                  type: "POST",
                  data: "delete="+id+"&db=alpha",
                  cache: false,
                  success: function(html) {
                    $("#alpha_notes").empty();
                    $("#alpha_notes").prepend(html);
                  }
                });
        }
}

function pushNote(id,name) {
                $.ajax({
                url: "/dbserver.php",
                        type: "POST",
                        data: "push="+id+"&db=alpha",
                        cache: false,
                        success: function(html) {
                            //
                          }
                });
}

</script>
</head>
<body>
        <h1 align=center>
        Sync 'em up.
        </h1>
        
<div id="container" style="width:700px;">
        <div id="logo">
                <img src="images/new_logo.gif">
        </div>
        <form method="POST" action="<?= $PHP_SELF ?>">
        <div id="select_db" style="width:500px;float:left;padding:10px;">
                <span style="background-color: yellow;padding:5px;">Select a 
database:</span>
                        <select name="location">
                <option value="alpha">alpha</option>
            </select>
        <span><h3>Alpha's current notes</h3></span>

                <div id="alpha_notes"></div>

    </div>
        </form>
        <div id="brocas" style="width:500px;padding:10px;">
                <span style="background-color: yellow;padding:5px;">Select notes
from Brocas to sync:</span>
        </div>
                <div id="brocas_notes">
        </div>
</div> <!-- container -->

</body>
</html>

--
dbserver.php

<?php
/**
 * server-side processing for dbsync.php
 */
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

// database connection variables
$username = 'root';
$password = 'apasswordogeshere!';
$host = 'localhost';
$db_brocas = 'phoenix_brocas';
$db_alpha = 'phoenix_alpha';

// if $_POST['db'], pull stuff from alpha and pop it
// into the alpha_notes table.
$brocas_notes = array();
$alpha_notes = array();

// connect to database
$link = mysql_connect($host, $username, $password)
    or die('Could not connect to database. Contact steve for
assistance!' . mysql_error());

    $db = @mysql_select_db($db_brocas, $link);
    if(!$link) {
        die('Yay, something is broken. Call Steve.');
    }
    $sql = "SELECT NAME,ID from ms_notes";
    $result = @mysql_query("$sql");

    if(mysql_num_rows($result) == 0) { echo "John, there's currently
zero notes on brocas."; }
    if(!$result) { die("Something is broken... "); }

    while($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {
$brocas_notes[] = $row; }

    // Fetch notes that are currently available on alpha
    $db = mysql_select_db($db_alpha, $link);
    if(!$db) {
        die("Can't connect to alpha, please contact steve.");
    }
    $alpha_results = @mysql_query("$sql");
//    if(mysql_num_rows($alpha_results)==0) { echo "There are
currently no notes in Alpha's database."; }
    while($row = @mysql_fetch_array($alpha_results, MYSQL_ASSOC)) {
$alpha_notes[] = $row; }
    if(!$alpha_results) { die("Something is broken... "); }

if($_POST['db']=='alpha') {
        if(mysql_num_rows($alpha_results) == 0) { echo "John, there's
currently zero notes on alpha.";  exit;}

// at this stage we should already have the available
// alpha notes from the code above. Let's populate the HTML
// to spit back into $html.

$html = '<table>';
$html .= '<tr>';
        // count here basically just keeps track of a
        // neatly aligned table
        $count=0;
    foreach($alpha_notes as $note) {
        $html .= "<td><a href=\"javascript:void(0);\"
onclick=\"removeNote('".$note[ID]."','".$note[NAME]."')\">delete</a>&nbsp;&nbsp;$note[NAME]</td>";
        $count++;
        if(($count % 2 == 0)) {
                $html .= "</tr><tr>";
        }
        }
$html .= '</tr></table>';

// send this sucker back to the view.
echo $html;
}

// If brocas has notes, which it always should,
// spit them back to <div id="brocas_notes">
if($_POST['db']=='brocas') {
$html = '<table>';
$html .= '<tr>';
        // count here basically just keeps track of a
        // neatly aligned table
        $count=0;
    foreach($brocas_notes as $note) {
        $html .= "<td><a href=\"javascript:void(0);\"
onclick=\"pushNote('".$note[ID]."','".$note[NAME]."')\">push</a>&nbsp;&nbsp;$note[NAME]</td>";
        $count++;
        if(($count % 2 == 0)) {
                $html .= "</tr><tr>";
        }
        }
$html .= '</tr></table>';
echo $html;
}

if($_POST['delete']) {
        global $alpha_notes;
        $db = mysql_select_db($db_alpha, $link);
    if(!$db) {
        die("Can't connect to alpha, please contact steve.");
    }

    $sql = "DELETE from ms_notes
                        WHERE ID='".$_POST['delete']
                ."' LIMIT 1";

    $result = mysql_query("$sql");
        if(!$result) { die("Something is broken... "); }
        
        $sql = "SELECT NAME,ID from ms_notes";
    $result = @mysql_query("$sql");
    if(mysql_num_rows($result)==0) { $html = "There are zero notes
left to process in Alpha's database"; echo $html; exit; }
        
        $html = '<table>';
        $html .= '<tr>';
                // count here basically just keeps track of a
                // neatly aligned table
                $count=0;
            foreach($alpha_notes as $note) {
                $html .= "<td><a href=\"javascript:void(0);\"
onclick=\"removeNote('".$note[ID]."','".$note[NAME]."')\">delete</a>&nbsp;&nbsp;$note[NAME]</td>";
                $count++;
                if(($count % 2 == 0)) {
                        $html .= "</tr><tr>";
                }
                }
        $html .= '</tr></table>';
        
        // send this sucker back to the view.
        echo $html;
}

if($_POST['push']) {
        $sql = "INSERT into phoenix_alpha.ms_notes
                                (SELECT * from phoenix_brocas.ms_notes
                                        WHERE ID='".$_POST['push']
                                        ."' LIMIT 1)";
                                        
        $result = mysql_query("$sql");
        if(!$result) { die("Something is broken... "); }
}

?>

If someone doesn't mind helping me clean this up, that would be great. :-)

On 9/24/07, Steve Finkelstein <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> So I'm trying to do something rather simple, but having difficulty
> accomplishing it. I'm basically giving the user an option to delete a
> row from a table within a div, using something similar to this on the
> server-side:
>
>         if(mysql_num_rows($result) == 0) { exit; }
>
>         $html = '<table>';
>         $html .= '<tr>';
>                 // count here basically just keeps track of a
>                 // neatly aligned table
>                 $count=0;
>             foreach($alpha_notes as $note) {
>                 $html .= "<td><a href=\"javascript:void(0);\"
> onclick=\"removeNote('".$note[ID]."','".$note[NAME]."')\">delete</a>&nbsp;&nbsp;$note[NAME]</td>";
>                 $count++;
>                 if(($count % 2 == 0)) {
>                         $html .= "</tr><tr>";
>                 }
>                 }
>         $html .= '</tr></table>';
>
> My issue is, if the user has deleted their 'last' available row, I
> want something to come up such as  "This table has no more information
> left." I tried doing that with the following:
>
> function removeNote(id,name) {
>         if(confirm("Are you sure you wish to delete: "+name+"?")) {
>                 // ajax code here to remove note.
>                 $.ajax({
>                   url: "/dbserver.php",
>                   type: "POST",
>                   data: "delete="+id+"&db=alpha",
>                   cache: false,
>                   success: function(html) {
>                         if(html==="FALSE") {
>                                 $("alpha_notes").empty().insert("There are no 
> notes left in alpha.");
>                         } else {
>                     $("#alpha_notes").empty().append(html);
>                         }
>                   }
>                 });
>         }
> }
>
> Any idea where I went wrong here? I'm assuming it's causing I'm
> returning a data type of HTML so my if() construct here doesn't
> logically make sense.
>
> - sf
>

Reply via email to