Jump to content

Recommended Posts

Hi,

 

Im trying to make some scaffolding with mysql and I need to know how to tell if a mysql field is a date/datetime/text/varchar etc.

 

<?php

?>
<link href="cms.css" rel="stylesheet" type="text/css" />
<?php

class Scaffold {

    var $table = "";

    function Scaffold($table){
        $this->table = $table;
        $connection = mysql_connect("localhost",
                            "ir",
                            "ir");
        mysql_select_db("test", $connection);

        switch($_GET["action"]){
            default:
                $this->listtable();
            break;

            case "list":
                $this->listtable();
            break;

            case "new":
                $this->newrow();
            break;

            case "postnew":
                $this->postnew();
            break;

            case "edit":
                $this->editrow();
            break;

            case "postedit":
                $this->postedit();
            break;

            case "delete":
                $this->deleterow();
            break;
        }
    }

    function listtable(){
        $select = mysql_query("SELECT * FROM ".$this->table);
        $i = 0;
        echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"cmstable\">";
        echo "<tr>";
        while($i < mysql_num_fields($select)){
            $column = mysql_fetch_field($select, $i);
            $column->name = str_replace("_"," ",$column->name);
            if($column->name != "id"){
                echo "<td><b>".$column->name."</b></td>";
            }
            $i++;
        }
        echo "</tr>";

        while($array = mysql_fetch_array($select)){
            echo "<tr>";
            foreach($array as $column => $value){
                if(!is_int($column) && $column != "id"){
                    echo "<td>$value</td>";
                }
            }
            ?>
                <td><a href="?action=edit&id=<?=$array[0]?>">Edit</a> <a href="?action=delete&id=<?=$array[0]?>">Delete</a></td>
            <?
            echo "</tr>";
        }

        echo "</table>";
        echo "<a href='?action=new'>New Row</a>";
    }

    function newrow(){

	include("fckeditor/fckeditor.php");

	$select = mysql_query("SELECT * FROM ".$this->table);
        $i = 0;
        echo "<form action='?action=postnew' method='POST'><table>";
        while($i < mysql_num_fields($select)){
            $column = mysql_fetch_field($select);
            $column->name = str_replace("_"," ",$column->name);
            if($column->name != "id"){
            print_r($column);
                if($column->blob == 1){
                    echo "<tr><td><b>$column->name:</b></td><td> <textarea name='$column->name'></textarea></td></tr>";
      /*              echo "<tr><td><b>$column->name:</b></td><td>";
				$oFCKeditor = new FCKeditor('$column->name');
				$oFCKeditor->BasePath = 'http://localhost:81/scaf2/fckeditor/';
				$oFCKeditor->Value = 'Default text in editor';
				$oFCKeditor->ToolbarSet = 'Basic';
				$oFCKeditor->Create();
				echo "</td></tr>";
        */        }
                else if($column->date == 1){
                    echo "<tr><td>$column->name:</td><td>  <input type='text' name='$column->name' /></td></tr>";
                }else{
                    echo "<tr><td><b>$column->name:</b></td><td>  <input type='text' name='$column->name' /></td></tr>";
                }
            }
            $i++;
        }
        echo "<tr><td colspan=2><input type='submit' value='Add Row' /></td></tr>";
        echo "</table></form>";
    }

    function postnew(){
        $select = mysql_query("SELECT * FROM ".$this->table);
        $insert = "INSERT INTO ".$this->table." VALUES('', ";
        $i = mysql_num_fields($select);
        $i--;
        foreach($_POST as $key => $value){
            $i--;
            if($i > 0){
                $insert .= "'".$value."', ";
            }
            else {
                $insert .= "'".$value."')";
            }
        }
        mysql_query($insert) or die(mysql_error());
        echo "Added row.<br />";
        echo "<a href='?action=list'>Back To Listing</a>";
    }

    function editrow(){
        $fields = mysql_query("SELECT * FROM ".$this->table) or die(mysql_error());
        $select = mysql_query("SELECT * FROM ".$this->table." WHERE id = ".$_GET["id"]) or die(mysql_error());
        $row = mysql_fetch_row($select);
        $i = 0;
        echo "<form action='?action=postedit&id=".$_GET["id"]."' method='POST'><table>";
        while($i < mysql_num_fields($fields)){
            $field = mysql_fetch_field($fields);
            $field->name = str_replace("_"," ",$field->name);
            if($field->name != "id"){
                if($field->blob == 1){
                    echo "<tr><td><b>$field->name:</b></td><td> <textarea name='$field->name'>".$row[$i]."</textarea></td></tr>";
                }
                else {
                    echo "<tr><td><b>$field->name:</b></td><td> <input type='text' name='$field->name' value='".$row[$i]."' /></td></tr>";
                }
            }
            $i++;
        }
        echo "<tr><td colspan=2><input type='submit' value='Edit Row' /></td></tr>";
        echo "</table></form>";
    }

    function postedit(){

        $select = mysql_query("SELECT * FROM ".$this->table." WHERE id = ".$_GET['id']);
        $num = mysql_num_fields($select);
        $update = "UPDATE ".$this->table." SET ";
        $i = 1;
        while($i <= $num){
            $column = mysql_fetch_field($select);
            if($column->name != "id"){
                if($i != $num){
                    $update .= $column->name." = '".$_POST["$column->name"]."', ";
                }
                else {
                    $update .= $column->name." = '".$_POST["$column->name"]."' WHERE id = ".$_GET['id'];
                }
            }
            $i++;
        }
        mysql_query($update) or die(mysql_error());
        echo "Edited row.<br />";
        echo "<a href='?action=list'>Back To Listing</a>";

    }

    function deleterow(){

        mysql_query("DELETE FROM ".$this->table." WHERE id = ".$_GET["id"]) or die(mysql_error());
        echo "Annihilated row.<br />";
        echo "<a href='?action=list'>Back To Listing</a>";

    }

}

?>

 

Thanks in advance

 

PyraX

Link to comment
https://forums.phpfreaks.com/topic/72717-solved-oop-table-scaffold-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.