Displaying form Items under categories - [php]

Discussion in 'PHP' started by sulphar, Nov 2, 2010.

  1. #1
    I am a new guy here and don,t have much knowledge of PHP. My problem is complex but let me explain it in simple lines.
    I have two different codes , 1) This code takes form entries 'website','caption' and 'name' and stores it into the Table named as 'user_info' into mysql database. This page is saved as index.php

    2) This code can add add new category as a root or a child of existing category with update(edit & delete) option.This page is saved as categories.php

    Both codes are working(except upload function),but what i want to do is to combine the both forms (Items and category) into one page so that a user can add :

    webite :Its any URL
    caption: Description of website
    name: name of the person
    Add category:choosing existing category
    Add new category:If not exist add new category

    Tell me how can i combine both codes so that my item entries display under their categories????

    1) code for items -stores in Table 'user_info'

    
    <html>
    
    <head>
    <style type="text/css">
      body { margin-left: 10.5%; margin-right: 10%; margin-top: 1%;}
    </style>
    <style type="text/css">
    .italic {
    font-style:italic;
    color: #336666;
    font-family: Calibri;
    padding:20px;
    
    
    
    }
    .bold {
    font-weight:bold;
    }
    </style> 
    <title>submit Bookmarks</title>
    </head>
    
    <body background="wrapper.png">
    
    <form action="index.php" method="post">
    
    <font face="Calibri">Add Your Links Here: </font>
    <input name="website" maxlength="100" size="58" value="http://www.">
    <p><font face="Calibri">Description:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </font>&nbsp;<input name="caption" maxlength="100" size="58">
    
    </p>
    <p><font face="Calibri">Your Name: </font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="name" maxlength="100" size="28">&nbsp;&nbsp;
    
    <input type="submit" name="Submit" value="Submit">&nbsp; <font size="4">&nbsp;</font></p>
    <p><font size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    OR&nbsp;&nbsp; </font><font color="#008080" size="2"><b><a href="soon.html">
    <span style="text-decoration: none"><font color="#008080" face="Calibri">Upload</font></span></a></b></font>|<i><font size="2" face="Calibri">upload your 
        Bookmarks from your computer. e.g., , .Csv,.txt or .mdb files.</font></i></p>
    <form action='upload.php' method='post'>
        <input type="hidden" name="sel_file"  />
        <font face="Calibri">Choose a file:&nbsp;</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="uploaded_file" type="file" />
        <input type="submit" value="Upload" />
      </form> </p>
    </form>
    
    
    
    </body></html>
    <?php 
     
    $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
    $db_user = "username"; // change to your database password 
    $db_password = "Password"; // change to your database password 
    $database = "database-name"; // provide your database name 
    $db_table = "user_info"; //  
    
    
    # STOP HERE 
    #################################################################### 
    # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
    $db = mysql_connect($hostname, $db_user, $db_password); 
    mysql_select_db($database,$db); 
    ?> 
    <html> 
    <head> 
    <title>Add Your Bookmarks Here</title> 
    </head> 
    <body> 
    
    
    <?php 
    if (isset($_REQUEST['Submit'])) { 
    # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
    $sql = "INSERT INTO $db_table(name,website,caption) values ('".mysql_real_escape_string(stripslashes($_REQUEST['name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['website']))."','".mysql_real_escape_string(stripslashes($_REQUEST['caption']))."')"; 
    $query_auto = "INSERT INTO user_info (entry_date, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )";
    if($result = mysql_query($sql ,$db)) { 
    echo 'Thank you, your information has been entered into our database<br><br>'; 
    } else { 
    echo "ERROR: ".mysql_error(); 
    } 
    } else { 
    ?>
     
    <?php 
    } 
    ?> 
    
    <?php
        if(isset($_POST['SUBMIT']))
        {
             $fname = $_FILES['sel_file']['name'];
            
             $chk_ext = explode(".",$fname);
            
             if(strtolower($chk_ext[1]) == "csv")
             {
            
                 $filename = $_FILES['sel_file']['tmp_name'];
                 $handle = fopen($filename, "r");
           
                 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                 {
                    $sql = "INSERT into user_info(name,website,caption) values('$data[0]','$data[1]','$data[2]')";
                    mysql_query($sql) or die(mysql_error());
                 }
           
                 fclose($handle);
                 echo "Successfully Imported";
             }
             else
             {
                 echo "Invalid File";
             }   
        }
    
        
    ?> 
    <?php
      
    
    
    
    $result = mysql_query("select * from user_info ORDER BY CUR_TIMESTAMP desc"); // Database Query result
    
    
    ?>
    <?php
    
    while ($row = mysql_fetch_array ($result)) {
    $i++;
    if ($i & 1) {
    echo "
    <div class='color1'>
    
    <br/>
    <span class='bold'>Website:</span> <span class='normalfont' ><a href='" . $row['website'] . "'>" . $row['website'] . "</a></span>
    <br/>
    
    <span class='bold'>Description:</span> <span class='normalfont'>" . $row['caption'] . " </span><span class='italic'>Added by:  " . $row['name'] . "</span>
    
    
    <br/>
    </div>";
    }
    else {
    echo "
    <div class='color2'>
    <br/>
    <br/>
    
    <span class='bold'>Website:</span> <span class='normalfont'><a href='" . $row['website'] . "'>" . $row['website'] . "</a></span>
    <br/>
    
    <span class='bold'>Description:</span> <span class='normalfont'>" . $row['caption'] . "</span><span class='italic'>Added by:  " . $row['name'] . "</span>
    
    
    <br/>
    <br/>
    </div>";
    }
    }
    ?>
    
    
    
    
    </body>
    
    PHP:

    2) Code for Adding Categories
    
    <?php error_reporting(E_ALL); ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Categories Class</title>
    <style>
    .code_div{
    border:dashed #006600 1px;
    background:#E8FFEB;
    font-family:Georgia, "Times New Roman", Times, serif;
    font-size:11px;
    width:90%;
    padding:5px;
    }
    </style>
    </head>
    <body style="font-family:Tahoma">
    <p>
      <?php
    
    
    // connect to database
    $hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost 
    $db_user = "username"; // change to your database password 
    $db_password = "password"; // change to your database password 
    $database = "batabase-name"; // provide your database name 
    $db_table = "categories"; // leave this as is 
    
    
    # STOP HERE 
    #################################################################### 
    # THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 
    $db = mysql_connect($hostname, $db_user, $db_password); 
    mysql_select_db($database,$db); 
    
    require_once("categories.class.php");
    
    // Simple Usage
    if(!isset($_GET["id"])) $_GET["id"] =0;
    
    $ctg_id = $_GET["id"];
    
    $categories = new categories;
        $categories->name_prefix = "&nbsp;&nbsp;";
    
        ?>
    </p>
    <p>Here is it a Html Menu:</p>
    <br>
    <div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; </font></font><br><font color="#000000"><font color="#ff8000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000bb">$output &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000bb">$output</font><font color="#007700">;</font></font></code> </div>
    <p>  
      <?
        // lets display the categories
            
            $output          = $categories->html_output($ctg_id);
            echo $output;    
        ?>
    </p>
    <hr>
    <p>now I'll modify the $HtmlTree value a bit.</p>
    <div class="code_div"> <code><font color="#000000"><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">HtmlTree </font><font color="#007700">= array( <br>
    </font><font color="#dd0000">"header" </font><font color="#007700">=&gt; </font><font color="#dd0000">"&lt;table width=200px border=0 cellpadding=2 cellspacing=2&gt;"</font><font color="#007700">, <br>
    </font><font color="#dd0000">"BodyUnselected" </font><font color="#007700">=&gt; </font><font color="#dd0000">'&lt;tr&gt;&lt;td  bgcolor=#C4D9FD &gt;[prefix]&amp;raquo;&lt;a  href="?id=[id]"&gt;&lt;font  color=#53507A&gt;[name]&lt;/font&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;'</font><font color="#007700">, <br>
    </font><font color="#dd0000">"BodySelected" </font><font color="#007700">=&gt; </font><font color="#dd0000">'&lt;tr&gt;&lt;td  bgcolor="#E4DB2C"&gt;[prefix]&amp;bull;&lt;a  href="?id=[id]"&gt;&lt;strong&gt;&lt;font  color="#000000"&gt;[name]&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;'</font><font color="#007700">, <br>
    </font><font color="#dd0000">"footer" </font><font color="#007700">=&gt; </font><font color="#dd0000">'&lt;/table&gt;'</font><font color="#007700">, <br>
    ); <br>
    <br>
    </font><font color="#0000bb">$output </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">html_output</font><font color="#007700">(</font><font color="#0000bb">$ctg_id</font><font color="#007700">); <br>
    echo </font><font color="#0000bb">$output</font><font color="#007700">; </font></font></code> </div>
    
    
    <?php
    
    $categories->HtmlTree = array(
    "header" => "<table width=200px border=0 cellpadding=2 cellspacing=2>", 
    "BodyUnselected" => '<tr><td bgcolor=#C4D9FD >[prefix]&raquo;<a href="?id=[id]"><font color=#53507A>[name]</font></a></td></tr>',
    "BodySelected" => '<tr><td bgcolor="#E4DB2C">[prefix]&bull;<a href="?id=[id]"><strong><font color="#000000">[name]</font></strong></a></td></tr>',
    "footer" => '</table>',
    );
    
    $output = $categories->html_output($ctg_id); 
    echo $output;
    ?>
        <hr>
        <?
        
        // lets get an array of the categories for our works
    $categories = new categories;
        $categories_list = $categories->build_list();
        
    // lets do some actions
    if(!isset($_REQUEST['act'])) $_REQUEST['act'] = "";
    
    $act     = $_REQUEST["act"];
    
    switch($act)
    {
        case "add":
        
            // lets add new category
            //  $categories->add_new( category parent ,  category name , description , icon path , group )
    
            $categories->add_new($_POST['parent'] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] );
            echo '<script>alert("Category was inserted successfully into database");
                  location="class_categories_test.php";    
                  </script>';
        break;
        
        case "delete":
            $categories->delete($_GET["id"]);
                echo '<script>alert("Category and Sub-Categories was successfully deleted");
                  location="class_categories_test.php";    
                  </script>';
        
        break;
        
        case "_update":
            $cat = $categories->fetch($_GET["id"]);
            
            ?>
            <form name="form1" method="post" action="">
              <p> the form below will execute the following. <br>
    </p>
              <div class="code_div"> <code><font color="#000000"><font color="#007700">&nbsp;</font><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">update</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"id"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"parent"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] );</font></font></code> </div>
              <br>
              <table width="90%"  border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
          <td colspan="3"><div align="center">Edit Category </div></td>
        </tr>
        <tr>
          <td width="13%">Child Of : </td>
          <td width="1%">:</td>
          <td width="86%">
          <select name="parent" id="parent">
            <option value="0" selected>ROOT</option>
            <?
            foreach($categories_list as $c)
            {
                if($cat["id"] == $c["id"]) continue; // we don't list the category to be child of itself !!
                ?>
                <option value="<?=$c["id"]?>" <? if($c["id"] == $cat["parent"]) echo "selected"; ?> ><?=$c["prefix"]?>&raquo;<?=$c["c_name"]?></option>
                <?    
            }
            ?>
          </select>
          </td>
        </tr>
        <tr>
          <td>Name:</td>
          <td>:</td>
          <td><input name="name" type="text" id="name" value="<?=$cat["c_name"]?>" size="20"></td>
        </tr>
        <tr>
          <td>Description:</td>
          <td>:</td>
          <td><textarea name="desc" cols="40" rows="3" id="desc"><?=$cat["c_desc"]?>
          </textarea></td>
        </tr>
        <tr>
          <td>Icon:</td>
          <td>:</td>
          <td><input name="icon" type="text" id="icon" value="<?=$cat["c_icon"]?>" size="30"></td>
        </tr>
        <tr>
          <td colspan="3"><div align="right">
            <input name="act" type="hidden" value="update">
            <input name="id" type="hidden" value="<?=$ctg_id?>">
            <input type="submit" name="Submit" value="Save">
          </div></td>
        </tr>
      </table>
    </form>
             <?
             die();
            
        break;
        
        case "update":
            $categories->update($_POST["id"] , $_POST["parent"] , $_POST["name"] , $_POST["desc"] , $_POST["icon"] );
            
            echo '<script>alert("Category was updated successfully!");
                  location="class_categories_test.php";    
                  </script>';
        break;
        
    }
    
    
        
    ?>
            
             <p align="center">&nbsp;</p>
             <p align="center"><strong>You may list all your categories expanded like this</strong></p>
    <div class="code_div">
      <code><font color="#000000"><font color="#0000bb">$categories </font><font color="#007700">= new </font><font color="#0000bb">categories</font><font color="#007700">; <br>
      &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000bb">$categories_list </font><font color="#007700">= </font><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">build_list</font><font color="#007700">(0);</font></font></code>
      <br>// $categories_list is now an array and you should use foreach command to print something like the following list.
    </div>
            <br>
             <table width="90%"  border="0" align="center" cellpadding="2" cellspacing="0">
            <tr>
                <td bgcolor="#4180BE"><font color="#FFFFFF"><strong>
                Categories List:
                </strong></font></td>
            </tr>
            <?
                foreach($categories_list as $c)
                {
                    ?>
                    
                    <tr>
                        <td>
                        
                        <?=$c["prefix"]?>&raquo;<?=$c["c_name"]?> - [<a href="?act=_update&id=<?=$c["id"]?>">Edit</a> - <a href="?act=delete&id=<?=$c["id"]?>">Delete</a>]
                        </td>
                    </tr>
                    
                    <?
                }
            ?>
            </table>
            
    
    
    <p align="center">&nbsp;</p>
    <p align="center"><strong>Or you may also use it in a combo box</strong></p>
    <table width="0" border="0" align="center" cellpadding="2" cellspacing="0">
      <tr>
        <td width="72"><select name="category_id" id="category_id">
        
          <?
            foreach($categories_list as $c)
            {
                ?>
                 <option value="<?=$c["id"]?>"><?=$c["prefix"]?>&raquo;<?=$c["c_name"]?> </option>
          <?     
            }
            ?>
                </select></td>
        <td width="0"><input type="button" name="Button" value="Edit" onClick="location='class_categories_test.php?act=_update&id='+document.getElementById('category_id').value;"></td>
        <td width="0"><input type="button" name="Button" value="Delete" onClick="location='class_categories_test.php?act=delete&id='+document.getElementById('category_id').value;"></td>
      </tr>
    </table>
    <p>&nbsp; </p>
    <p><hr></p>
    <form name="form1" method="post" action="">
     the form below will execute the following.
      <br>
    <div class="code_div">
      <code><font color="#000000"><font color="#ff8000">&nbsp;</font><font color="#0000bb">$categories</font><font color="#007700">-&gt;</font><font color="#0000bb">add_new</font><font color="#007700">(</font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">'parent'</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"name"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"desc"</font><font color="#007700">] , </font><font color="#0000bb">$_POST</font><font color="#007700">[</font><font color="#dd0000">"icon"</font><font color="#007700">] ); </font></font></code></div><br>
      <table width="400"  border="0" align="center" cellpadding="2" cellspacing="0">
        <tr>
          <td colspan="3" bgcolor="#4180BE"><div align="center"><strong><font color="#FFFFFF">Add New Category </font></strong></div></td>
        </tr>
        <tr>
          <td width="13%">Child Of : </td>
          <td width="1%">:</td>
          <td width="86%"><select name="parent" id="parent">
            <option value="0">ROOT</option>
            <?
            foreach($categories_list as $c)
            {
                ?>
            <option value="<?=$c["id"]?>"  >
            <?=$c["prefix"]?>&raquo;<?=$c["c_name"]?>
            </option>
            <?    
            }
            ?>
          </select></td>
        </tr>
        <tr>
          <td>Name:</td>
          <td>:</td>
          <td><input name="name" type="text" id="name" size="20"></td>
        </tr>
        <tr>
          <td>Description:</td>
          <td>:</td>
          <td><textarea name="desc" cols="40" rows="3" id="desc"></textarea></td>
        </tr>
        <tr>
          <td>Icon:</td>
          <td>:</td>
          <td><input name="icon" type="text" id="icon" value="http://" size="30"></td>
        </tr>
        <tr>
          <td colspan="3"><div align="right">
            <input name="act" type="hidden" value="add">
            <input type="submit" name="Submit" value="Add">
          </div></td>
        </tr>
      </table>
     
    </form>
    
    <p align="center">&nbsp;</p>
    
    </body>
    </html>
    
    PHP:
    Sorry for making this long post,hope u won,t mind :) and will solve my problem
     
    sulphar, Nov 2, 2010 IP
  2. backlinkneeded

    backlinkneeded Member

    Messages:
    285
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    wowo, this is long. I think you should offer little money to fix this. atleast $5
     
    backlinkneeded, Dec 22, 2010 IP