Jump to content

ludwigvb

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Posts posted by ludwigvb

  1. Hello,

    I have made a form where I can update all the entries of my mysql database table.
    I have made it so it makes the fields automaticly.
    How can I get the variables back to update the mysql table?

    Here is the code:
    (read the comments to know what I mean)

    [code]<?php

    include 'config.php';
    include 'menu.php' ;

    $db = mysql_connect($server_name,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $sql="SELECT * FROM links ORDER BY cat_id ASC, name ASC";
    mysql_query("SET NAMES 'utf8'");
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    $rows=mysql_fetch_array($result) ;
    ?>
    <table width="500" border="0" cellspacing="1" cellpadding="0" align="center">
    <form name="form1" method="post" action="">
    <tr>
    <td align="center">
    <?php echo $lang_links_edit ; ?>
    <table width="90%" border="0" cellspacing="1" cellpadding="0">

    <?php
    echo'
    <tr>
    <td colspan="10" align="center"><a href="links_edit.php">'. $lang_links_edit .'</a></td>
    </tr>
    <tr class="odd">
    <td align="center"><strong>'. $lang_cat_id .'</strong></td>
    <td align="center"><strong>'. $lang_url .'</strong></td>
    <td align="center"><strong>'. $lang_links_name .'</strong></td>
    <td align="center"><strong>'. $lang_link_cat_name .'</strong></td>
    <td align="center"><strong>'. $lang_link_submiter .'</strong></td>
    <td align="center"><strong>'. $lang_link_subemail .'</strong></td>
    <td align="center"><strong>'. $lang_link_back .'</strong></td>
    <td align="center"><strong>'. $lang_desc .'</strong></td>
    <td align="center"><strong>'. $lang_link_approval .'</strong></td>
    <td align="center"><strong>'. $lang_delete .'</strong></td>
    </tr> ';

    $i=0 ;
    while($i < $count){

    $id=mysql_result($result,$i,"id");
    $url=mysql_result($result,$i,"url");
    $description=mysql_result($result,$i,"description");
    $name=mysql_result($result,$i,"name");
    $submiter=mysql_result($result,$i,"submiter");
    $email=mysql_result($result,$i,"email");
    $link_back=mysql_result($result,$i,"link_back");
    $cat_id=mysql_result($result,$i,"cat_id");
    $approved=mysql_result($result,$i,"approved");
    ?>
    <tr>
    <td align="center"><? echo $id ; ?></td>
    <td align="center"><input name="url_<?php echo $id ; ?>" type="text" id="url" value="<? echo $url ; ?>"></td>
    <td align="center"><input name="name_<?php echo $id ; ?>" type="text" id="name" value="<? echo $name ; ?>"></td>

    <td align="center">
    <select name="cat_id_<?php echo $id ; ?>">

    <?php // GET CURRENT CATEGORY NAME

    // $db = mysql_connect($server_name,$user,$password);
    // @mysql_select_db($database) or die( "Unable to select database");

    $id_ap = $id ;
    $sql_edit="SELECT * FROM links WHERE id='$id_ap'";
    mysql_query("SET NAMES 'utf8'");
    $result_edit=mysql_query($sql_edit);
    $row_edit=mysql_fetch_array($result_edit);

    $cat_id_now= $row_edit['cat_id'];
    $sql_cat_name="SELECT * FROM categories WHERE cat_id='$cat_id_now'";
    mysql_query("SET NAMES 'utf8'");
    $result_cat_name=mysql_query($sql_cat_name);
    $row_cat_name=mysql_fetch_array($result_cat_name);

    echo '<option value="'.$row_cat_name['cat_id'].'">'.$row_cat_name['cat_name'].'</option>';

    // GET CATEGORIES NAME TO CREATE LIST
    $sql2="SELECT * FROM categories";
    $result2=mysql_query($sql2);

    $count_cats=mysql_num_rows($result2);

    $j=0;
    while ($j < $count_cats) {

    $cat_ids=mysql_result($result2,$j,"cat_id");
    $cat_names=mysql_result($result2,$j,"cat_name");

    echo '

    <option value="'.$cat_ids.'">'.$cat_names.'</option>

    ';
    $j++ ;
    }

    echo '</select></td>';
    ?>

    <td align="center"><input name="submiter_<?php echo $id ; ?>" type="text" id="submiter" value="<? echo $submiter ; ?>"></td>
    <td align="center"><input name="email_<?php echo $id ; ?>" type="text" id="email" value="<? echo $email ; ?>"></td>
    <td align="center"><input name="link_back_<?php echo $id ; ?>" type="text" id="link_back" value="<? echo $link_back ; ?>"></td>
    <td align="center"><input name="description_<?php echo $id ; ?>" type="text" id="description" value="<? echo $description ; ?>"></td>


    <td align="center"><input name="approved_<?php echo $id ; ?>" type="checkbox" id="approved" value="1" <?php if ($approved == "1") { echo "checked=\"checked\""; } ?>></td>



    <td align="center"><input name="del_<?php echo $id ; ?>" type="checkbox" id="del" value="1"></td>

    </tr>
    <?php
    $i++ ;
    }
    ?>
    <tr>
    <td colspan="10" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for($i=0;$i<$count;$i++){


    // WHAT SHOULD I PUT HERE TO CREATE THE VARIABLES THAT GET POSTED FROM THE FORM???? <<<<<<<<<<<<=======================
    $id = $id_.''.$id ;
    $url = $url_.''.$id ;
    $description = $description_.''.$id ;
    $name = $name_.''.$id ;
    $submiter = $submiter_.''.$id ;
    $email = $email_.''.$id ;
    $link_back = $link_back_.''.$id ;
    $cat_id = $cat_id_.''.$id ;
    $approved = $approved_.''.$id ;
    $del = $del_.''.$id ;
    // WHAT SHOULD I PUT HERE TO CREATE THE VARIABLES THAT GET POSTED FROM THE FORM???? <<<<<<<<<<<<=======================


    echo $id .','.$url .','.$description .','. $name .','. $submiter .','. $email .','. $link_back .','. $cat_id .','. $approved .','. $del .'<br>' ;

    $sql3="UPDATE links SET id='$id', url='$url', description='$description', name='$name', submiter='$submiter', email='$email', link_back='$link_back', cat_id='$cat_id', approved='$approved' WHERE id='$id'";
    mysql_query("SET NAMES 'utf8'") or DIE("ERROR");
    $result3=mysql_query($sql3) or DIE("ERROR");
    }

    // echo '<META HTTP-EQUIV="Refresh"
    //      CONTENT="0; URL=links_edit.php">';
    }

    mysql_close();
    ?>[/code]
  2. [!--quoteo(post=378451:date=May 30 2006, 08:48 PM:name=phpPrincess)--][div class=\'quotetop\']QUOTE(phpPrincess @ May 30 2006, 08:48 PM) [snapback]378451[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    ok its working but the results arent displaying next to the question!!1 banging head on desk- im so over this
    [/quote]


    That's is a matter of HTML.
    Try making tables to include text and variables.
    Since everything is going to be sent as an e-mail too, don't forget to change the header information of the e-mail to html
  3. [!--quoteo(post=378423:date=May 30 2006, 07:21 PM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ May 30 2006, 07:21 PM) [snapback]378423[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    ok that would explain how to seperate them,

    but what about understanding how many have been seperated? in order to have a count for a loop , which would then insert each seperated item into the sql query. Are they saved as an array?

    How would I go about doing this?

    Thanks

    D
    [/quote]

    Maybe this can help:
    [a href=\"http://www.phpfreaks.com/quickcode/Building-a-Dynamic-Form-Select-Menu/2.php\" target=\"_blank\"]http://www.phpfreaks.com/quickcode/Buildin...lect-Menu/2.php[/a]
  4. [code]<textarea name="NOTE" rows="20" cols="85" tabindex="1"><?php echo 'AAAAA';?></textarea> [/code]

    Then read:

    [a href=\"http://www.tizag.com/mysqlTutorial/index.php\" target=\"_blank\"]PHP - mySQL tutorial[/a]

    to edit, update, delete data from mySQL database!


    [!--quoteo(post=378429:date=May 30 2006, 07:50 PM:name=natalieG)--][div class=\'quotetop\']QUOTE(natalieG @ May 30 2006, 07:50 PM) [snapback]378429[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    we have a frorm we want to initalize the fields, edit the fields, and then uipdate thew database.We have
    tried a buch of things and cannot get there for a tgextarea control. Our latesst attempt
    is below.

    [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]<td width="664" COLSPAN="2"><textarea name="NOTE" rows="20" cols="85" tabindex="1" CONTENT="<?php echo 'AAAAA';?>"></textarea></td>[!--colorc--][/span][!--/colorc--]

    Thanks,

    Jennifer
    [/quote]
  5. [code] echo ' My Company\'s information';[/code]
    or
    [code] echo "My Company's information";[/code]

    [!--quoteo(post=378428:date=May 30 2006, 07:43 PM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ May 30 2006, 07:43 PM) [snapback]378428[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    Hi,

    I want to echo out the following...

    echo ' My Company's information';

    but because there's an ' included PHP picks up an error.

    How can i include an '

    Thanks

    D
    [/quote]
  6. [!--quoteo(post=378421:date=May 30 2006, 07:28 PM:name=phpPrincess)--][div class=\'quotetop\']QUOTE(phpPrincess @ May 30 2006, 07:28 PM) [snapback]378421[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    WHAT AM I DOING WRONG!!! ITS NOT WORKING!!!

    YOU GUYS WANT THE WHOLE CODE AGAIN
    [/quote]

    HERE IS THE RIGHT CODE:

    FILE:
    WhatDoYouThink.php

    [code]
    <form method="post" action="sendeail.php">
    <?php
    $ipi = getenv("REMOTE_ADDR");
    $httprefi = getenv ("HTTP_REFERER");
    $httpagenti = getenv ("HTTP_USER_AGENT");
    ?>

    <input type="hidden" name="ip" value="<?php echo $ipi ?>" />
    <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
    <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
    <table width="500">
    <tr>
    <td width="319"><label><span class="style1">How often do you visit MOSI?</span></label></td>
    </tr>
    <tr>
    <td><span class="style5">
    <input type="radio" name="Q1" value="First visit">
    First visit</span></td>
    </tr>
    <tr>
    <td><span class="style5">
    <label>
    <input type="radio" name="Q1" value="Less than once a year">
    Less than once a year</label>
    </span></td>
    </tr>
    <tr>
    <td><span class="style5">
    <label>
    <input type="radio" name="Q1" value="About once a year">
    About once a year</label>
    </span></td>
    </tr>
    <tr>
    <td><span class="style5">
    <label>
    <input type="radio" name="Q1" value="More than once a year">
    More than once a year</label>
    </span></td>
    </tr>
    </table>
    <br>
    <p align="left" class="style1">
    <label></label>
    </p>
    <table width="500">
    <tr>
    <td><label><span class="style1">How often do you visit museums in general?</span></label></td>
    </tr>
    <tr>
    <td><input type="radio" name="Q2" value="Less than once a year">
    Less than once a year</td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q2" value="About once a year">
    About once a year</label></td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q2" value="A few times a year">
    A few times a year</label></td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q2" value="Once a month or more">
    Once a month or more</label></td>
    </tr>
    </table>
    <br>
    <table width="500">
    <tr>
    <td><label>What is your gender? </label></td>
    </tr>
    <tr>
    <td><input type="radio" name="Q3" value="Male">
    Male</td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q3" value="Female">
    Female</label></td>
    </tr>
    </table>
    <br>
    <table width="500">
    <tr>
    <td><label>What is your age group? </label></td>
    </tr>
    <tr>
    <td><input type="radio" name="Q4" value="15-24">
    15-24</td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q4" value="25-34">
    25-34</label></td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q4" value="35-54">
    35-54</label></td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q4" value="55-64">
    55-64</label></td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q4" value="65 and over">
    65 and over</label></td>
    </tr>
    </table>
    <p align="left" class="style1">&nbsp; </p>
    <table width="500">
    <tr>
    <td><label>Do you currently live in Tampa? </label></td>
    </tr>
    <tr>
    <td><input type="radio" name="Q5" value="Yes">
    Yes</td>
    </tr>
    <tr>
    <td><label>
    <input type="radio" name="Q5" value="No">
    No</label></td>
    </tr>
    </table>
    <p align="left" class="style1">Including yourself, how many persons are there in your group or family visiting the museum with you today?<br>
    <label>
    <select name="Q6" size="1">
    <option selected>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6 or more</option>
    </select>
    </label>
    </p>
    <p align="left" class="style1">How many of those are aged 12 or older, again, including yourself?<br>
    <label>
    <select name="Q7">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6 or more</option>
    </select>
    </label>
    </p>

    Your Email:<br />
    <input type="text" name="visitormail" size="35" />
    <br />
    <br />
    <br />
    Mail Message: <br />
    <textarea name="notes" rows="4" cols="40"></textarea>
    <br />
    <input type="submit" value="Send Mail" />
    </form>[/code]

    File: sendeail.php

    [code]

    <!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>..::..RateMyBoyfriend..::..</title>
    <style type="text/css">
    <!--
    body {
    background-color: #424242;
    background-image: url();
    }
    .style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
    -->
    </style></head>

    <body>
    <table width="200" border="0" align="center">
    <tr>
    <td width="490" colspan="4"><p>&nbsp;</p>
    <p>&nbsp;</p>
    <table width="433" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="433">

    <?php

    $myemail = "erinkuzma@yahoo.com";


    if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
    {
    echo "<h2>Use Back - Enter valid e-mail</h2>\n";
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    }
    if(empty($visitormail) || empty($notes )) {
    echo "<h2>Use Back - fill in all fields</h2>\n";
    }
    echo $badinput;

    $todayis = date("l, F j, Y, g:i a");

    $attn = $attn;
    $subject = $attn;

    $notes = stripcslashes($notes);

    $message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n
    From: $visitor ($visitormail)\n

    ";
    $A1="How often do you visit MOSI?". $Q1 ."\r\n";
    $A2="How often do you visit museums in general?". $Q2 ."\r\n";
    $A3="What is your gender?". $Q3 ."\r\n";
    $A4="What is your age group?". $Q4 ."\r\n";
    $A5="Do you currently live in Tampa?". $Q5 ."\r\n";
    $A6="Including yourself, how many persons are there in your group or family visiting the museum with you today?". $Q6 ."\r\n";
    $A7="How many of those are aged 12 or over, again, including yourself?". $Q7 ."\r\n";

    $from = "From: $visitormail\r\n";

    if ($myemail != "")
    for ($i=1; $i<8; $i++) {
    $message .= ${'A' . $i};;
    }

    if ($myemail != "")
    mail($myemail, $subject, $message, $from);

    ?>
    <p align="left" class="style1">
    <strong>Date:</strong> <?php echo $todayis ?>
    <br />
    <strong>Thank You :</strong> ( <?php echo $visitormail ?> )
    <br />

    <br />
    <strong>Message:</strong><br />
    <?php $notesout = str_replace("\r", "<br/>", $notes);
    echo $notesout; ?>
    <br />
    <?php echo $A1 ?> <br>
    <?php echo $A2 ?><br>
    <?php echo $A3 ?><br>
    <?php echo $A4 ?><br>
    <?php echo $A5 ?><br>
    <?php echo $A7 ?><br><br>
    <?php echo $A7 ?><br>



    <?php echo $ip ?>

    <br /><br />
    <a href="WhatDoYouThink.php"> Send another message.. </a></p> </td>
    </tr>
    </table> <p>&nbsp;</p></td>
    </tr>
    </table>[/code]

    At least it's working for me!!!
  7. poirot you are right, but this should make more sense to phpPrincess

    YOUR code is shorter and that means that is more correct.

    [!--quoteo(post=378414:date=May 30 2006, 06:52 PM:name=phpPrincess)--][div class=\'quotetop\']QUOTE(phpPrincess @ May 30 2006, 06:52 PM) [snapback]378414[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    am i keeping this
    for ($i=1; $i<8; $i++) {
    $message .= ${'Q' . $i};;
    }
    [/quote]


    Yes keep this.
    It has the same results that my code has and it makes your file smaller!
  8. Try changinf this code:

    [code]$message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n
    From: $visitor ($visitormail)\n

    ";
    $Q1="How often do you visit MOSI? $Q1\r\n";
    $Q2="How often do you visit museums in general? $Q2 \r\n";
    $Q3="What is your gender? $Q3 \r\n";
    $Q4="What is your age group? $Q4 \r\n";
    $Q5="Do you currently live in Tampa? $Q5 \r\n";
    $Q6="Including yourself, how many persons are there in your group or family visiting the museum with you today? $Q6 \r\n";
    $Q7="How many of those are aged 12 or over, again, including yourself? $Q7 \r\n"; [/code]

    to:

    [code]$message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n
    From: $visitor ($visitormail)\n

    ";
    $Q1="How often do you visit MOSI? $Q1\r\n";
    $Q2="How often do you visit museums in general? $Q2 \r\n";
    $Q3="What is your gender? $Q3 \r\n";
    $Q4="What is your age group? $Q4 \r\n";
    $Q5="Do you currently live in Tampa? $Q5 \r\n";
    $Q6="Including yourself, how many persons are there in your group or family visiting the museum with you today? $Q6 \r\n";
    $Q7="How many of those are aged 12 or over, again, including yourself? $Q7 \r\n";

    $message .= $Q1
    $message .= $Q2
    $message .= $Q3
    $message .= $Q4
    $message .= $Q5
    $message .= $Q6
    $message .= $Q7

    [/code]

    and:

    [code]mail($myemail, $attn, $subject, $message, $Q1, $Q2, $Q3, $Q4, $Q5, $Q6, $Q7, $from);[/code]

    to:
    [code]mail($myemail, $attn, $subject, $message, $from);[/code]

    or:

    [code]mail($myemail, $subject, $message, $from);[/code]
  9. I tried this just before your post... it worked (ofcourse)

    I spent 2 hours tryign to figure out what was wrong.

    Another question is:

    Am I stupid?

    BUT I have to answear this by my own !!! [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

    Thanks for your help [b]poirot[/b]
  10. [!--quoteo(post=378388:date=May 30 2006, 06:05 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ May 30 2006, 06:05 PM) [snapback]378388[/snapback][/div][div class=\'quotemain\'][!--quotec--]
    This is a bit odd. How can a row have cat_id=71 AND cat_id=72 AND... ?
    [/quote]

    The table "categories" I have is like this:

    cat_id | cat_name
    70 | name01
    71 | name02
    72 | name03


    If I executre the query:

    [code]DELETE FROM categories WHERE cat_id=71 AND cat_id=72 AND cat_id=70[/code]

    should it delete all the rows that have the specific cat_id s?
  11. Helloz,

    I'm trying to make a command to delete some mysql rows in a script.
    The code I use is:

    [code]
    $subcats = catDelete($cat_id); // gets the subcategories of the top category

    echo $subcats; //  just a test to see that it outputs the right code
    echo '<br>'. $cat_id; // just a test to see the the $cat_id variable has not been changed

        $db = mysql_connect($server_name,$user,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        mysql_query("DELETE FROM categories WHERE $subcats cat_id = $cat_id",$db) or die(mysql_error());  

    echo $lang_info_del; // echos that the categories and subcategories has been deleted
    [/code]

    echo commands are to make sure I get the right code for the mysql query that deletes the rows
    $subcats echos:
    cat_id=71 AND cat_id=72 AND

    When I try to delete a category that has sub categories rows are not getting deleted
    When I try to delete a category with no subcategories the command is executed and the row gets deleted.

    Any tips?
    Thanks in advance!

    Spyros
  12. First of all I would like to thank reakus_maximus and Barand for their replies.

    Barands code is the right one. The solution has been found!!!!

    Here I 'll post a some kind of tutorial of:

    [b]How to create a php - mysql script to create categories with unlimited sub categories[/b]

    1. Create a mySQL database
    2. Create a table in it named categories (you can execute the following code in phpadmin)

    [code]
    CREATE TABLE `categories` (
      `cat_id` int(4) NOT NULL auto_increment,
      `cat_name` varchar(255) collate utf8_unicode_ci default NULL,
      `parent_cat_id` int(4) NOT NULL default '0',
      `root_cat_id` int(4) NOT NULL default '0',
      `sort_order` bigint(8) NOT NULL default '0',
      `cat_img` varchar(255) collate utf8_unicode_ci default NULL,
      `date_added` datetime NOT NULL default '0000-00-00 00:00:00',
      `date_edit` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
      PRIMARY KEY  (`cat_id`),
      KEY `parent_cat_id` (`parent_cat_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0;
    [/code]

    I have set UTF-8 for multilanguage support

    3. Now create a file and name it [b]categories.php[/b]

    Put the code:

    [code]
    <?php

    include 'config.php';
    ?>

    <HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?php echo $encoding; ?>" />
    <title><?php echo $lang_titletag; ?></title>
    <link href="css.css" rel="stylesheet" type="text/css" />
    </head>
    <body>

    <?php

    //**************************//
    //    ADD NEW CATEGORY START    //
    //**************************//

    if ($add) { //if add button is released

    // Connect to db and set encoding
        $db = mysql_connect($server_name,$user,$password);
        @mysql_select_db($database) or die( "Unable to select database");
        mysql_query("SET NAMES 'utf8'");

    if ($parent_cat_id != 0){ // if a category is selected a sub category should be added

    // select the parent's_id parent_id until parent_cat_id and root_cat_id is ZERO (0)
        $query=mysql_query("SELECT * FROM categories WHERE cat_id=$parent_cat_id",$db);
        $row=mysql_fetch_array($query);

    $parent_cat_name = $row["cat_name"];
    $parent_cat_loop = $row["parent_cat_id"];
    $root_cat_loop = $row["root_cat_id"];

        while ($parent_cat_loop !=0 && $root_cat_loop !=0) { // start searching the root cat
        $query=mysql_query("SELECT * FROM categories WHERE cat_id=$parent_cat_loop",$db);
        $row=mysql_fetch_array($query);
        $parent_cat_loop = $row["parent_cat_id"];
        $root_cat_loop = $row["root_cat_id"];
        } // root cat has been found
        $root_cat_id = $row["cat_id"];

    // insert subcategory to db
        $sql = "INSERT INTO categories (cat_id, cat_name, parent_cat_id, root_cat_id, sort_order, cat_img, date_added, date_edit) VALUES ('', '$cat_name', '$parent_cat_id', '$root_cat_id', '$sort_order', '$cat_img', '$date_added', '$date_added')";
        $result = mysql_query($sql);



        echo '<div align="center">'.$lang_cat_added .' '. $parent_cat_name.'</div>';

        } // subcategory has been added

    else if ($parent_cat_id == 0) { // no category has been selected

    // insert root category to db
        $sql = "INSERT INTO categories (cat_id, cat_name, parent_cat_id, root_cat_id, sort_order, cat_img, date_added, date_edit) VALUES ('', '$cat_name', '$parent_cat_id', '$root_cat_id', '$sort_order', '$cat_img', '$date_added', '$date_added')";
        $result = mysql_query($sql);


        echo '<div align="center">'.$lang_root_cat_added.'</div>';
        } // root category has been added
    } // end if ($add)
    ?>

    <?php

    unset($root_cat_id, $parent_cat_id);

    $db = mysql_connect($server_name,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    mysql_query("SET NAMES 'utf8'");


    $query="SELECT * FROM categories WHERE parent_cat_id = 0 ORDER BY cat_name";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);


    if ($num==0) {
    echo '';
    } else {

    function catOptions ($parent, $level=0) {

    include 'config.php';
    $db = mysql_connect($server_name,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    mysql_query("SET NAMES 'utf8'");

        $sql = "SELECT cat_id, cat_name FROM categories WHERE parent_cat_id = '$parent' ORDER BY cat_name";

        $res = mysql_query($sql) or die(mysql_error());
        $str = '';
        while (list($id, $name) = mysql_fetch_row($res)) {
            $indent = str_repeat('---', $level);
            $str .= "<OPTION value='$id'>$indent $name</OPTION>\n";

               // now call itself to get subcats of this cat
              $str .= catOptions($id, $level+1);
        }
        return $str;
    }

    }

    ?>

    <form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF?>">

    <input type="hidden" name="date_added" value="<?php echo date('Y-m-d G:i:s'); ?>">

    <table width="500" border="0" align="center" cellspacing="2">
      <tr>
        <td width="200" bgcolor="#CCCCCC"><?php echo $lang_cat_name; ?> </td>
        <td width="300" bgcolor="#CCCCCC"><input type="Text" name="cat_name"></td>
      </tr>
      <tr>
        <td bgcolor="#ADD8E6"><?php echo $lang_sort_order; ?></td>
        <td bgcolor="#ADD8E6"><input type="Text" name="sort_order"></td>
      </tr>
      <tr>
        <td bgcolor="#CCCCCC"><?php echo $lang_cat_img; ?></td>
        <td bgcolor="#CCCCCC"><textarea name="cat_img" wrap rows="12" cols="25" ></textarea></td>
      </tr>
      <tr>
        <td bgcolor="#ADD8E6"><?php echo $lang_cat_sub; ?></td>
        <td bgcolor="#ADD8E6">
        <?php echo "<SELECT name='parent_cat_id'> \n";
    echo '<option value="0">&nbsp;';
    echo catOptions(0);
    echo "</SELECT>\n";
    ?>

    </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><br>
          <input type="Submit" name="add" value="<?php echo $lang_cat_add; ?>"></td>
      </tr>
    </table>
    </form>
    <?php
    //**************************//
    //    ADD NEW CATEGORY END    //
    //**************************//
    ?>
        </body>
    </html>
    [/code]

    I have put some comments on it to figure out the steps

    4. Save the file in UTF-8 format

    5. Now create another file called [b]config.php[/b] and put in it:
    [code]
    <?php
    @setlocale(LC_TIME, 'el_GR.UTF8');

    // change the following variables according to your settings
    $user="your_db_username"; // username for the database
    $password="your_db_password"; // password for the database
    $server_name="localhost"; // server name.. most of the times - localhost
    $database="your_db_name"; // the database

    $encoding="utf-8";

    include "english.php";

    ?>
    [/code]
    Change what needs to be changed :)
    Save in UTF-8 format

    6. Create another file [b]english.php[/b] (or another language but remember to change it in [b]config.php[/b])

    Put this code in it:

    [code]
    <?php
    $lang_titletag = "Categories Administration";


    // Categories

    $lang_cat_added = "Category ".$cat_name." has been added under ";
    $lang_cat_name = "Category name";
    $lang_sort_order = "Category sorting<br>(enter number)";
    $lang_cat_img = "Category image";
    $lang_cat_sub = "Choose parent category";
    $lang_cat_add = "Add Category";

    ?>
    [/code]

    Save in UTF- format

    7. Upload, Check if it is working, let me know :)

    [b]I'm using a wysiwyg editor for the image textarea[/b]
    You can use a file upload input if you prefer.

    GOOD LUCK

    Spyros
  13. Hi,

    I have made this table to store categories and subcategories for a script I'm making:
    [img src=\"http://www.hostin.gr/mysqltable.jpg\" border=\"0\" alt=\"IPB Image\" /]

    How can I sort it in a drop down (select) form menu like this?

    cat 01
    --cat_01_01
    ----cat_01_01_01
    ------cat_01_01_01_01
    --------cat_01_01_01_01_01
    ----cat_01_01_02
    cat 02
    --cat_02_01
    --cat_02_02
    cat 03
    cat 04
    --cat 04_01
    cat 05
    --cat_05_01

    I have used the code:
    [code]$query="SELECT * FROM categories WHERE parent_cat_id = 0 ORDER BY cat_name";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);

    // mysql_close();

    if ($num==0) {
    echo '';
    } else {

    $i=0;
    while ($i < $num) {
        
    $cat_id=mysql_result($result,$i,"cat_id");
    $cat_name=mysql_result($result,$i,"cat_name");
    $root_cat_id=mysql_result($result,$i,"root_cat_id");
    $parent_cat_id=mysql_result($result,$i,"parent_cat_id");

    $categories_list .= '<option value="'.$cat_id.'">'.$cat_name.'';


    $query02="SELECT * FROM categories WHERE parent_cat_id=$cat_id ORDER BY cat_name";
    $result02=mysql_query($query02);
    $num02=mysql_num_rows($result02);




    if ($num02 == 0) {
        echo '';
    }
    else {
    $j=0;
    while ($j < $num02) {

    $cat_id02=mysql_result($result02,$j,"cat_id");
    $cat_name02=mysql_result($result02,$j,"cat_name");
    $root_cat_id02=mysql_result($result02,$j,"root_cat_id");
    $parent_cat_id02=mysql_result($result02,$j,"parent_cat_id");

    $categories_list .= '<option value="'.$cat_id02.'">  '.$cat_name02.'';
    $j++;
            }
    }
    $i++;
        }
    }
    mysql_close();[/code]

    But in this way only 2 levels of categories and subcategories are shown, so I will have to repeat the code so many times as the depth of subcategories.

    Any help will be really appriciated.

    Thanks in advance.

    Spyros
×
×
  • 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.