Jump to content

Skatecrazy1

Members
  • Posts

    126
  • Joined

  • Last visited

Posts posted by Skatecrazy1

  1. Hi, well I'm creating a news script that will serve two purposes:

    -display news on a user's website
    -teach beginning-intermediate php coders the basics of using MySQL

    I'm trying to make the code as simple as possible, so the latter objective can be achieved.
    here's the first thing I created: the installation file.  I wanted to keep everything within one file, yet still keep the code looking simple, however I haven't been able to achieve that.

    Keep in mind all the code I show you here is fully functional.

    I was wondering if someone here could show me things I could change/replace to make the code more readable by those who have a limited understanding of PHP.

    [code]
    <?php
    //install.php
    /*Installs the Database tables, gets the username and
    password of the main admin*/
    /*also asks for the server, username, and password
    of the mysql for the user */
    $self = $_SERVER['PHP_SELF'];
    //SQL STUFF*/
    $id = $_GET['act'];
    if(isset($id)){
                  if($id == "sqlinfo"){
    $host = $_POST['server'];
    $user = $_POST['server_user'];
    $pass1 = $_POST['pass1'];
    $pass2 = $_POST['pass2'];
    $db = $_POST['db'];
          if(isset($host) && isset($user)){
            if($pass1 == $pass2){
          $filename = "db.php";
          $file = fopen($filename, "w+");
          $dbfile = "
          <?php
          \$conn = @mysql_connect(\"$host\", \"$user\", \"$pass1\") or die(\"Sorry, could not connect to MySQL.\");
          \$rs = @mysql_select_db(\"$db\") or die(\"Sorry, couldn't select MySQL database.  Make sure you entered the correct database name.\");
          ?>";
          fwrite($file, $dbfile);
          header("location:$self?act=sqlsetup");
            } else {
              echo("<font color=\"red\"><b>Passwords Did not Match</b></font>");
              }
          }
                  $msg = "<form method=\"post\" action=\"$self?act=sqlinfo\">";
                  $msg .= "<h3>MySQL Server Information</h3>";
                  $msg .= "<p>Before you install the script, we need some basic
                  MySQL information so that the news system can successfully
                  be installed on your MySQL server.</p>";
                  $msg .= "<table cellspacing=\"2\" cellpadding=\"0\">";
                  $msg .= "<tr>";
                  $msg .= "<td>";
                  $msg .= "MySQL Server<br />
                  <small>If you are not sure, just leave as \"localhost\"</small>";
                  $msg .= "</td><td>";
                  $msg .= "<input type=\"text\" name=\"server\" value=\"localhost\" />";
                  $msg .= "</td></tr>";
                  $msg .= "<tr><td>";
                  $msg .= "Database Name";
                  $msg .= "<br /><small>This is where all your data will go.  Make sure this is correct.</small>";
                  $msg .= "</td><td><input type=\"text\" name=\"db\" /></td>";
                  $msg .= "</tr>";
                  $msg .= "<tr><td>";
                  $msg .= "MySQL Username<br />
                  <small>This username was most likely given to you by your sysadmin.</small>";
                  $msg .= "</td><td>";
                  $msg .= "<input type=\"text\" name=\"server_user\" />";
                  $msg .= "</td></tr>";
                  $msg .= "<tr><td>";
                  $msg .= "MySQL Password";
                  $msg .= "</td><td>";
                  $msg .= "<input type=\"text\" name=\"pass1\" />";
                  $msg .= "</td></tr><tr>";
                  $msg .= "<td>Repeat Password</td><td>";
                  $msg .= "<input type=\"text\" name=\"pass2\" />";
                  $msg .= "</td></tr><tr>";
                  $msg .= "<td colspan=\"2\"><input type=\"submit\" value=\"Install\" /></td>";
                  $msg .= "</tr></table>";
                  $msg .= "</form>";
                  //echo out that really long form we just created
                  echo $msg;
                  }
                 
          elseif($id == "sqlsetup"){
            require('db.php');

            $sql = array();
           
            $sql[0] = "
            CREATE TABLE `users` (
            id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            username TEXT NOT NULL,
            password TEXT NOT NULL,
            access_level TINYINT NOT NULL,
            email TEXT NOT NULL,
            aim TEXT NOT NULL,
            msn TEXT NOT NULL,
            yahoo TEXT NOT NULL,
            website TEXT NOT NULL
            )";
           
            $sql[1] = "
            CREATE TABLE `posts` (
            id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            title TEXT NOT NULL,
            body TEXT NOT NULL,
            timestamp TEXT NOT NULL,
            author TEXT NOT NULL,
            comment_amt INT NOT NULL
            )";
           
            $sql[2] = "
            CREATE TABLE `comments` (
            id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            name TEXT NOT NULL,
            body TEXT NOT NULL,
            email TEXT NOT NULL,
            postid INT NOT NULL
            )
            ";
           
            $sql[3] = "
            CREATE TABLE `styles` (
            id TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            css TEXT NOT NULL
            )
            ";
           
            foreach($sql as $node)
            {
              @mysql_query($node, $conn) or die("Installation failed.");
              }
            sleep(3);
            echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=usrreg\">");
           
           
          }
         
          elseif($id == "usrreg"){
          require('db.php');
          $username = $_POST['username'];
          $pass1 = $_POST['password'];
          $pass2 = $_POST['passconf'];
          if(isset($username)){
            if($pass1 == $pass2){
              $sql = "INSERT INTO `users` (username, password, access_level) VALUES(
              '$username',
              '$pass1',
              '1'
              )";
              @mysql_query($sql, $conn) or die("Installation failed.");
              echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=finished\">");
              } else {
              echo("<font color=\"red\"><b>Passwords did not match.</b></font>");
              }
            }
          $form = "<h3>Final Step</h3>";
          $form .= "<form method=\"post\" action=\"$self?act=usrreg\">";
          $form .= "<p><small>Before EzNews can be put into action, you must first specify your administrative
          username and password, so you may log into the control panel.</small></p>";
          $form .= "<table cellspacing=\"2\" cellpadding=\"0\">";
          $form .= "<tr><td>Desired Username</td><td><input type=\"text\" name=\"username\" /></td></tr>";
          $form .= "<tr><td>Desired Password</td><td><input type=\"password\" name=\"password\" /></td></tr>";
          $form .= "<tr><td>Confirm Password</td><td><input type=\"password\" name=\"passconf\" /></td></tr>";
          $form .= "<tr><td><input type=\"submit\" value=\"Finish\" /></td></tr>";
          $form .= "</table>";
          $form .= "</form>";
          echo $form;
          }
         
          elseif($id == "finished"){
          $msg = "<h3>Installation Complete!</h3>";
          $msg .= "<p>Installation of EzNews has been successful; you may now log into the
          administrative control panel with your selected username and password combination.</p>";
          $msg .= "<p><a href=\"admin/index.php\">Log In Now</a></p>";
          echo $msg;
          }
         
    } else {
    echo("<meta http-equiv=\"refresh\" content=\"0;url=$self?act=sqlinfo\">");
    }
    ?>
    [/code]
  2. well, when i run the query SELECT DATEDIFF('2006-11-21','2006-11-24') in phpMyAdmin, it returns 3, but what I need now is to be able to echo that info for each row (in my list of tasks)

    I can't figure out how to do it (trust me, I've been trying different things)
  3. Well, I'm creating a sort of task manager, that shows the start date of a task, the end date of a task, the task name, and the duration.  The latter is what I'm having problems with:

    I've tried and tried but can't seem to get the difference between the start and end date (in the yyyy-mm-dd mysql format).  has anyone done this sort of thing before?
  4. I've never coded an entire one before, but what I can tell you is modularize everything, so that you can add things later.  You'll always regret it if you can't add things on afterwards.
  5. wheee

    [code]
    <?php  $self = $_SERVER['PHP_SELF'];
              $operator = $_POST['radio'];
              $value1 = $_POST['value1'];
              $value2 = $_POST['value2'];
    if(isset($operator)){
      if($operator == "*"){
        $final_value = $value1 * $value2;
      }
      elseif($operator == "/"){
        $final_value = $value1 / $value2;
      }
      elseif($operator == "+"){
        $final_value = $value1 + $value2;
      }
      elseif($operator == "-"){
        $final_value = $value1 - $value2;
      }
    } else {
        $final_value = "";
    }

    ?>

    <form method="post" action="<?php echo $self; ?>">
    First Value <input type="text" name="value1" />
    <br />
    + <input type="radio" name="radio" value="+" />
    - <input type="radio" name="radio" value="-" />
    x <input type="radio" name="radio" value="*" />
    / <input type="radio" name="radio" value="/" />
    <br />
    Second Value <input type="text" name="value2" />
    <br />
    <input type="submit" value="Calculate" />
    <br /><br />
    Final Value: <input type="text" value="<?php echo $final_value; ?>" />
    </form>
    [/code]

    see how what good use i put my time to....

    hooray.
  6. this has been tested and works:
    [code]
    <?php  $self = $_SERVER['PHP_SELF'];
              $operator = $_POST['radio'];
              $value1 = $_POST['value1'];
              $value2 = $_POST['value2'];
    if(isset($operator)){
      if($operator == "*"){
        $final_value = $value1 * $value2;
      }
      elseif($operator == "/"){
        $final_value = $value1 / $value2;
      }
      elseif($operator == "+"){
        $final_value = $value1 + $value2;
      }
      elseif($operator == "-"){
        $final_value = $value1 - $value2;
      }
    } else {
        $final_value = "";
    }

    ?>

    <form method="post" action="<?php echo $self; ?>">
    First Value <input type="text" name="value1" />
    <br />
    + <input type="radio" name="radio" value="+" />
    - <input type="radio" name="radio" value="-" />
    x <input type="radio" name="radio" value="*" />
    / <input type="radio" name="radio" value="/" />
    <br />
    Second Value <input type="text" name="value2" />
    <br />
    <input type="submit" value="Calculate" />
    <br /><br />
    Final Value: <input type="text" value="<?php echo $final_value; ?>" />
    </form>
    [/code]
  7. Okay so basically, it's how jwk811 put it, but theres a little more to it.  To put it clearly:

    let's say i have a variable named $str.

    up at the top of my file, $str = "Eric Cartman";

    let's say I want to change the value of $str later in my script:  $str = "Stan Marsh";

    but also, using the "." operator, i could add things to a string: $str .= " and Kenny McCormick";

    which would echo "Eric Cartman and Kenny McCormick"

    plus, if you include a file that contains a variable in another file, the variable is accessible in the file you included it to.
  8. well not really, i just made it out of boredom and wanted to show someone

    but yeah i could make it into a class and make it part of something bigger on some profile site.
  9. copy and paste the below code and have fun (i was bored)

    [code]
    <?php

    function h1($arg)
    {
    echo("<h1>$arg</h1>");
    }

    function br()
    {
    echo("<br>");
    }

    function generateRandTime()
    {
    $randhr = rand(1, 12);
    $randmin = rand(01, 59);
    $times = array("AM", "PM");
    $shuff_arr = shuffle($times);
    if(strlen($randmin) == 1){
    echo($randhr.":0".$randmin);
    } else {
    echo($randhr.":".$randmin);
    }
    echo " ".$times[0];
    }

    function generateRandomWeapon()
    {
    $weapons = array("candlestick", "rope", "knife", "revolver", "pipe");
    $shuff = shuffle($weapons);
    echo($weapons[0]);
    }

    function generateRandomSong()
    {
    $songs = array(
    "Amazing Grace",
    "Taps",
    "Dust in the Wind",
    "Angel",
    "All My Tears",
    "Be Not Afraid",
    "Bullet the Blue Sky",
    "Easter Parade",
    "Emily",
    "Face to Face",
    "Going Home",
    "Goodbye",
    "Home Free");
    $shuff = shuffle($songs);
    echo($songs[0]);
    }

    function generateRandomAttendance()
    {
    $att = rand(10, 50);
    echo($att);
    }

    function generateRandomSuicides()
    {
    $deaths = rand(0, 2);
    echo($deaths);
    }

    h1("Your Eminent Demise");
    echo("You die at ");
    generateRandTime();
    br();
    echo("You are killed with a ");
    generateRandomWeapon();
    br();
    echo("At your funeral, the song \"");
    generateRandomSong();
    echo("\" is played.");
    br();
    generateRandomAttendance();
    echo(" people attend your funeral.");
    br();
    generateRandomSuicides();
    echo(" people commit suicide because of your death.");



    ?>[/code]
  10. so if i'm correct, which i'm probably not, you're trying to keep a checkbox checked based on the value of a mysql field row.  very doable, in fact it was just something i came across.

    the way i did it was very simple, and it didn't involve using implode() at all.

    what i did was i had a field called `checked`, which either had a value of 0 for not checked, or 1 for checked. (tinyint)

    then, when i retrieved the row data, I did something like this to see whether I wanted to have the checkbox checked or not
    [code]
    <?php
    $conn = mysql_connect($server, $username, $password) or die(mysql_error());
    $rs = mysql_select_db($database_name) or die(mysql_error());
    $sql = "SELECT * FROM `table` WHERE `id`=\"$selected_id\"";
    $rs = mysql_query($sql, $conn) or die(mysql_error());
    //fetch array of row info
    $row = mysql_fetch_array($rs);
    //assuming the column is named "checked"
    $msg = "<input type=\"checkbox\" name=\"toggle\" ";
    if($row['checked'] == 1){
    $msg .= "checked ";
    }
    $msg .= "/>";
    echo $msg;
    ?>
    [/code]

    so if the column `checked` is set to 1, it echoes "checked" inside the input tag, but if not, it leaves that part out.

    this works for 1 single checkbox, but I'm sure if you improvise you can have different columns for different checkboxes and make it work out.
  11. yep fenway's got the idea.

    on your html:
    [code]
    <div id="container">Content here, this will be overflowabble (if that's a word) when you're done.
    </div>
    [/code]

    in the css:
    [code]
    #container {
    overflow: auto;
    }
    [/code]

    very simple.
×
×
  • 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.