Jump to content

Skatecrazy1

Members
  • Posts

    126
  • Joined

  • Last visited

Everything 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. [url=http://www.php.net/explode]http://www.php.net/explode[/url]
  3. 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)
  4. that's a little vague; are you referring to parsing XML?
  5. well, I'm not too sure about syntax, so let me throw something out there: SELECT DATEDIFF(`end_date`,`start_date`) FROM `table` doesn't seem to work, probably because it's not valid syntax...
  6. 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?
  7. 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.
  8. 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.
  9. well as far as having a form submit when you change something in a dropdown box, you can use something like onSelect="this.form.submit()"
  10. just use a comma to separate, like WHERE `field` != 'value', `secondfield` != 'secondexcludedvalue', `third` != 'thirdval' and so on
  11. 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]
  12. also, usually if you're just making a script with SQL that executes a query, and you're not retrieving data, you don't put the query into a variable, you just execute it.
  13. 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.
  14. no, seriously.  that's THE way to do it.  post your code so we can see what went wrong.
  15. what in hell are you guys talking about... ?  web 2.0?
  16. [code] <?php include('guestbook.php'); ?> [/code] put that wherever you want the guestbook 2 go in your layout
  17. no probably just something got restarted when you logged in in the morning.
  18. 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.
  19. 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]
  20. 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.
  21. hmm, well it works alright now, i can view the directory even with the .htaccess present.  I had restarted apache the last time I tried it, but it still didn't work; i restarted it a second time and it worked.  thanks.
  22. well if someone is requiring you to use the get method, then just do the same thing as above, but change your form method to url and replace $_POST with $_GET and it should work
  23. 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.