Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wildo

Members
  • Joined

  • Last visited

Everything posted by Wildo

  1. I don't that it would look good, I like the navigation as it is now. I just read some pen tool tutorials, doesn't look too hard, I'll start trying converting it with the pen tool thanks for the tip. Wildo
  2. Do you mean the menu text, or the 'Yoursite.com' thing and I don't really get what you mean by separator, you mean like a '|' (if you mean the navigation). Wildo
  3. Wildo replied to japjake's topic in General Chat
    ' this will come...' Wildo
  4. Wildo replied to japjake's topic in General Chat
    'we should stop' Wildo
  5. could you be more precise ? I mean in php I could just scream function called mysql_real_escape_string() bu you wouldn't understand that either if you don't have much experience Wildo
  6. Wildo replied to BenG's topic in General Chat
    I'll take the fifth on that one Wildo
  7. Yeah I know it looks pixel like... but since I don't 100% know the way around photoshop I couldn't think of a fix Thanks for the feedback, I think I'll finish the whole layout now Wildo
  8. Hi there, I was designing a bit today, I'm not too good at it, but I gave it another try anyway this was what I came up with, but I'm not to happy with the 'yoursite.com', I really like the concept but I'm kinda stuck on how to continue from this. anyone more advanced in designing could give me a heads up with suggestions on what to do? cheers, Wildo
  9. Wildo replied to BenG's topic in General Chat
    hmm can I showoff my 343 members on my msn list...? No? oh ok then I wont... Wildo
  10. Thanks for that, at least that was useful and easy to understand . Guess it's time to launch myself a portfolio site then, shouldn't be to hard Wildo
  11. Wildo replied to japjake's topic in General Chat
    'Forest gump. Why' Wildo
  12. glad to hear that, I hoped it was pretty straightforward, I guess I was wrong Wildo
  13. Wildo replied to japjake's topic in General Chat
    'very ugly pirate' Wildo
  14. Wildo replied to japjake's topic in General Chat
    'used toilet paper' Wildo
  15. Wildo replied to japjake's topic in General Chat
    'coming from a' Wildo
  16. Tutorial CMS – Part 3 So - I kinda forgot to post part 3, so here it is, sorry for the delay! Well then, it all comes down to this. The last part, part 3! In this part we will construct a admin panel, where you can Add(without staff approval), Edit and Delete tutorials. Nothing complicated just using simple php to make it happen. Note: we are now coding inside the case ‘admin’: and before the break; have fun. <?PHP // this admin panel is not secured by password or something like that, // if you want to integrate it with EG naresh’s user system, // add things like if (isset($logged[username]) && isset($logged[password])) { // we start off, by once again making a little framework to work in, // the admin section is divided in the 4 sections I told you about above print "<h3>Tutorial Admin</h3>\n"; // the admin panel coming up! print "<h2>Tutorials</h2>\n"; print "<a href=\"tutorial.php?act=Admin&mod=Add\" target=\"_self\">Add tutorial</a>"; print " | \n"; print "<a href=\"tutorial.php?act=Admin&mod=Approve\" target=\"_self\">Approve tutorials</a>"; print " | \n"; print "<a href=\"tutorial.php?act=Admin&mod=Edit\" target=\"_self\">Edit tutorials</a>"; print " | \n"; print "<a href=\"tutorial.php?act=Admin&mod=Delete\" target=\"_self\">Delete tutorials</a>"; print "<br /><br />\n"; print ""; // basically we display the navigation for the // admins, showing them 4 links of what to do $whereAdmin = $_GET[mod]; // using the $_GET[value] once more to use // the link to know what to display $adminArray = array("Add", "Approve", "Edit", "Delete"); // with array() we create another array with the possible admin actions // and assigning it to a variable! if ($whereAdmin == "Add") { // if the $whereAdmin variable is equal to 'Add', we do this: print "Add tutorial:<br /><br />\n"; // we display where we are. } elseif ($whereAdmin == "Approve") { // elseif the $whereAdmin variable is equal to 'Approve', we do this: print "Approve tutorials:<br /><br />\n"; // we print where we are } elseif ($whereAdmin == "Edit") { // elseif the $whereAdmin variable is equal to 'Edit', we do this: print "Edit tutorial:<br /><br />\n"; // we print where we are } elseif ($whereAdmin == "Delete") { // elseif the $whereAdmin variable is equal to 'Delete', we do this: print "Delete tutorial:<br /><br />\n"; // we print where we are } // so much for our framework, as you see we have created 4 // sections, each containing different admin options. // our link to the admin panel now looks like // tutorial.php?act=Admin&mod=Add // the filename -> Admin part -> section ?> I hope that that was not to hard for you lot. We continue by creating the content for the first section. ‘Add’. The Add section will allow staff to add tutorials without the tutorial being reviewed by another member of the staff. We are now coding inside the ‘Add’ section of the admin panel: <?PHP if ($whereAdmin == "Add") { // HERE } ?> Let’s throw some code in your face. <?PHP // if the $whereAdmin variable is equal to 'Add', we do this: // the add tutorial part is actually 90% similair to the normal add // page, but instead it will have $getApprove to 1 instead of 0, // and you get to set the acces level, // like 'All', 'Members' or only 'Staff' // however this system doesn't come with a user system // attached to it, so create your own user system // and use if statement to check if who can view the tutorial // if they cant tell 'm they are not allowed // nothing fancy, if you need help with that // just ask :) print "Add tutorial:<br /><br />\n"; // our usual print of where we are! if (!isset($_POST[posttut])) { // if the submit button has NOT been pressed, // we check that by adding a '!' in front of the isset() function // which checks if something has a value. print "<form method=\"post\">\n"; print "<strong>Title:</strong> \n<br />\n"; print "<input type=\"text\" name=\"title\"> \n<br />\n"; print "<strong>Subtitle:</strong> \n<br />\n"; print "<input type=\"text\" name=\"subtitle\"> \n<br />\n"; print "<strong>Author:</strong> \n<br />\n"; print "<input type=\"text\" name=\"author\"> \n<br />\n"; print "<strong>Category:</strong> \n<br />\n"; print "<select name=\"category\"> <option title=\"PHP\">PHP</option> <option title=\"HTML\">HTML</option> <option title=\"Photoshop\">Photoshop</option> </select>\n<br />\n"; print "<strong>Difficulty:</strong> \n<br />\n"; print "<select name=\"difficulty\"> <option title=\"Easy\">Easy</option> <option title=\"Medium\">Medium</option> <option title=\"Hard\">Hard</option> </select>\n<br />\n"; print "<strong>Acces:</strong> \n<br />\n"; print "<select name=\"acces\"> <option title=\"All\">All</option> <option title=\"Members\">Members</option> <option title=\"Staff\">Staff</option> </select>\n<br />\n"; print "<strong>Content:</strong> <br />\n"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> \n<br />\n"; print "<input type=\"submit\" name=\"posttut\" value=\"Add Tutorial\">\n"; print "</form>\n"; // easy pudding and pie form, } else { // oh the submit button HAS been pressed // let's process the information retrieved from the form // we start of by assigning the various posted // information into some variables $getTitle = $_POST[title]; $getSubTitle = $_POST[subtitle]; $getAuthor = $_POST[author]; $getCategory = $_POST[category]; $getDifficulty = $_POST[difficulty]; $getContent = $_POST[content]; $getAcces = $_POST[acces]; $getDate = date("Y-m-j"); // YYYY-MM-DD date format $getApprove = 1; // approval of 1, so it doesn't need any approval from staff $insertTutorial = mysql_query("INSERT INTO tutorials (title, subtitle, author, difficulty, category, postdate, approve, content, acces) VALUES ('$getTitle', '$getSubTitle', '$getAuthor', '$getDifficulty', '$getCategory', '$getDate', '$getApprove', '$getContent', '$getAcces')"); // a lovely query inserting the new tutorial to our database print "Thank you, your tutorial has been added to our database. You can now see it in the tutorial list!"; // ah lovely thank you message telling the user all went well. } ?> So that was the Add tutorial part of the admin panel, next up is the tutorial approval section. Let’s get going shall we! <?PHP // elseif the $whereAdmin variable is equal to 'Approve', we do this: // first of all, let me tell you how we are going to 'approve' tutorials, // we start of by making a list of all the pending tutorials, // we select all the pending tutorials by checking if the 'approve' row // is equal to '0'. // every tutorial that comes out of that query will have a link to a page // containing the full tutorial with a button to approve or decline // the tutorial, approve will make the 'approve' row to 1, // decline will make the row to '2', you can edit this, // by deleting the declined tutorial from the database, // but that is up to you :) print "Approve tutorials:<br /><br />\n"; // once again we display where we are! $aa = $_GET[section]; // we use $_GET once again to define what we want to display, the list // or the tutorial! if (isset($aa) && $aa = "Approve") { // if the $aa variable is set, // and is equal to 'Approve' // we display the full tutorial if (isset($_GET[aid]) && is_numeric($_GET[aid])) { // we check if ‘aid’ is set in the link, and if it is a numeric value, if it is: $tid = $_GET[aid]; // we assign the part of the URL ‘&aid=*’ to the $tid variable $a = mysql_query("SELECT * FROM tutorials WHERE approve = '0' AND id = '$tid' LIMIT 1"); // we make a query selecting the tutorial corresponding to the $tid value and with the approve value of ‘o’ if (mysql_num_rows($a) == 0) { // if there are no results returned from the query print "No <em>pending</em> tutorial corresponds with that ID<br />\n"; // error message! } else { // else, we have results! Let’s display it. if (!isset($_POST[pending])) { // if the form button has not been pressed, // the form button, contains the form data // to approve or decline a tutorial while ($b = mysql_fetch_object($a)) { // once again we grab all the data using mysql_fetch_object(); print $b->title."<br />\n"; print $b->subtitle."<br />\n"; print $b->category."<br />\n"; print $b->author."<br />\n"; print $b->content."<br /><br />\n"; // we print out the tutorial print "<form method=\"post\">\n"; print "<input type=\"radio\" name=\"pendingThis\" value=\"approve\" /> Approve <br /> <input type=\"radio\" name=\"pendingThis\" value=\"decline\" /> Decline<br />\n"; print "<input type=\"submit\" name=\"pending\" value=\"Submit\"><b r/>\n"; print "</form>\n"; // we show a form, allowing the admin to approve or decline the tutorial } } else { // the form button HAS been pressed, let’s see what the admin did. if ($_POST[pendingThis] == "approve") { // if he approved the tutorial $a = mysql_query("UPDATE tutorials SET approve = '1' WHERE id = '$tid'"); print "The tutorial has been approved, you can now view it in the tutorial list."; // we update the approve value of the tutorial with a query // and display a success message } else { // if he didn’t approve the tutorial, he declined it $a = mysql_query("UPDATE tutorials SET approve = '2' WHERE id = '$tid'"); print "The tutorial has been declined!"; // we update the approve value to 2, and display a success message } } } // we close some brackets } else { print "Invalid ID given!<br />\n"; // **** there was no id, or it was invalid!, we display an error:( } } else { // if $aa is not set, // we will display all // the pending tutorials! $a = mysql_query("SELECT * FROM tutorials WHERE approve = '0'"); // we select all the unapproved tutorials with the above query! if (mysql_num_rows($a) == 0) { // we check if the result is equal to 0, if it is: print "There are currently no pending tutorials!\n"; // sorry no tutorials :( } else { // Else, Yay we have tutorials that are pending! while ($b = mysql_fetch_object($a)) { // we fetch all the results into a object with variable $b print "<a href=\"tutorial.php?act=Admin&mod=Approve&section=Approve&aid=".$b->id."\" target=\"_self\">".$b->title."</a><br />\n"; print $b->category." | ".$b->author."<br />\n"; // and we print the results with a link to view and approve them } } } // we close some brackets! ?> The semi-last piece of code for you guys. The Edit part! No more talking, more coding, let’s go! <?PHP // what we are going to do: // we check if the admin has supplied us with an id, // if they did we display the edit tutorial form, // if they don’t we show a list of all the tutorials with // a link containing the id. // we check if they presses the submit button of the edit // tutorial. If they did, we edit the tutorial // with the new data retrieved from the form. $editId = $_GET[ei]; // and again we use the link to get some information this time, // we are asking for ‘ie’ wich is short for EditId. if (!$editId || !is_numeric($editId)) { // we check if the var $editId is not set or not numeric, if it is: $a = mysql_query("SELECT * FROM tutorials ORDER BY postdate DESC"); // we select all the tutorials from the database and sorting them by date while ($b = mysql_fetch_object($a)) { // ONCE again we use mysql_fetch_object(); to fetch the results into an object print "<b><a href=\"tutorial.php?act=Admin&mod=Edit&ei=".$b->id."\" target=\"_self\">".$b->title."</a></b><br />\n"; print $b->subtitle."<br />\n"; print $b->author." | ".$b->category." | ".$b->postdate."<br /><br />\n"; // we display some of the tutorial information, including a link to edit it. } } else { // if $editId is set and is numeric we continue with editing the tutorial. if (!$_POST[edittutorial]) { // if the Edit button has not been pressed $a = mysql_query("SELECT * FROM tutorials WHERE id = $editId"); while ($b = mysql_fetch_object($a)) { // we make a query and fetch the results with mysql_fetch_object(); print "<form method=\"post\">\n"; print "<strong>Title:</strong> <br />\n"; print "<input type=\"text\" value=\"".$b->title."\" name=\"title\"> <br />\n"; print "<strong>Subtitle:</strong> <br />\n"; print "<input type=\"text\" value=\"".$b->subtitle."\" name=\"subtitle\"> <br />\n"; print "<strong>Author:</strong> <br />\n"; print "<input type=\"text\" value=\"".$b->author."\" name=\"author\"> <br />\n"; print "<strong>Category:</strong> <br />\n"; print "<select name=\"category\"> <option title=\"PHP\">PHP</option> <option title=\"HTML\">HTML</option> <option title=\"Photoshop\">Photoshop</option> </select>\n<br />\n"; print "<strong>Difficulty:</strong> \n<br />\n"; print "<select name=\"difficulty\"> <option title=\"Easy\">Easy</option> <option title=\"Medium\">Medium</option> <option title=\"Hard\">Hard</option> </select>\n<br />\n"; print "<strong>Access:</strong> \n<br />\n"; print "<select name=\"acces\"> <option title=\"All\">All</option> <option title=\"Members\">Members</option> <option title=\"Staff\">Staff</option> </select>\n<br />\n"; print "<input type=\"hidden\" name=\"tutorialid\" value=\"".$b->id."\">\n"; print "<strong>Content:</strong> <br />\n"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$b->content."</textarea> <br />\n"; print "<input type=\"submit\" name=\"edittutorial\" value=\"Edit Tutorial\">\n"; print "</form>\n"; // we display the tutorial inside input fields, allowing the admin to edit it. } } else { // the edittutorial button has been pressed, let’s process the tutorial $title = $_POST[title]; $subtitle = $_POST[subtitle]; $author = $_POST[author]; $category = $_POST[category]; $content = $_POST[content]; $difficulty = $_POST[difficulty]; $acces = $_POST[acces]; $editIdtutorial = $_POST[tutorialid]; $a = mysql_query("UPDATE tutorials SET title = '$title', subtitle = '$subtitle', author = '$author', category = '$category', content = '$content', acces = '$acces', difficulty = '$difficulty' WHERE id = $editIdtutorial"); print "The tutorial <strong>".$title."</strong> has successfully been edited!"; // we assign various variables to the new data, and store the new data into the database // and we display a success message! } } ?> Now the final piece of code has yet to be revealed. Here it comes, the delete section of the admin panel, as usual I commented every line I though was important. <?PHP // elseif the $whereAdmin variable is equal to 'Delete', we do this: print "Delete tutorial:<br /><br />\n"; $delId = $_GET[di]; // and ONCE again we get a bit information from the URL with $_GET[di] if (!$delId || !is_numeric($delId)) { // if $delId is not set, or is not numeric we do this: $a = mysql_query("SELECT * FROM tutorials WHERE approve = ’1’ ORDER BY postdate DESC"); // a simple query selecting all the tutorials while ($b = mysql_fetch_object($a)) { // using a while loop and mysql_fetch_object we grab all the information into a object print "<b><a href=\"tutorial.php?act=Admin&mod=Delete&di=".$b->id."\" target=\"_self\">".$b->title."</a></b><br />"; print $b->subtitle."<br />\n"; print $b->author." | ".$b->category." | ".$b->postdate."<br /><br />\n"; // we display the vital information about the tutorial, // including a link to the tutorial if the admin // wants to see it before they delete it } } else { // else if the id is set and it is numeric: if (!$_POST[delnews]) { // if the submit button has not been pressed we do this: $a = mysql_query("SELECT * FROM tutorials WHERE id = $delId"); // we make yet another query select the tutorial with the corresponding id while ($b = mysql_fetch_object($a)) { // using a while loop and mysql_fetch_object we grab all the information into a object print "Are you sure you want to delete the article <strong>".$b->title."</strong>?"; print "<form method=\"post\">"; print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">"; print "</form>"; // displaying a simple form asking the admin if they are sure they want to delete it } } else { // yes the admin is sure to delete it, and has pressed the submit button. $a = mysql_query("DELETE FROM tutorials WHERE id = '$delId'") or die ("Could not delete the tutorial!"); // we delete the record from our database print "The article <strong>".$b->title."</strong> has successfully been deleted!"; // and we tell the admin that } } // we close some brackets and we are done! ?> Well well, so much for this tutorial, as always, questions/suggestions? You know where to ask. Oh wait, I promised the full working code. . . Hmm check the attachment! Wildo Tutorial_CMS_part_1_3.txt
  17. Tutorial CMS – Part 2 Well then, we finished part 1 without any bruises I hope? In part 2 we will make a List of all the tutorials in the ‘List’ case.. furthermore we are going to make the add page, any tutorials submitted there must go through approval of anyone that has access to the admin panel. So let’s go, I hope you still have that framework from part1 , we are going to code inside <?PHP case ‘List’: // HERE break; ?> Now. Have fun. As always I commented as hell, this part is a bit complicated but apart from some scratches and bruises we should be alright! Here we go <?PHP // first off, before we are going to show the user anything, we need to assign a lot of things to // make this work, As I’ve stated in part 1, the List case will include several sorting options, // you can sort by Category, Difficulty title you name it everything, you can even filter the // List so it only contains tutorials from a certain category! $url = "tutorial.php?act=List"; // we assign a value to the $url variable, containing the base URL for the List case $order = strtolower($_GET[sort_by]); // we, once again, use a $_GET[value] to get any information from the url, then we use // strtolower() function to remove any capital letters from the received information // strtolower() works like this: $bla = strtolower(BlA);, that will change BlA to bla // easy huh;) $list = array("category", "postdate", "author", "title", "difficulty", "rating", "views"); // then we use the array() function to make a array with all the fields we want to sort with // and we assign it to the $list variable! if (in_array($order, $list)) { // we check if the received information retrieved from the $_GET[sort_by] matches any of // words inside the array, if there is a match we do this: $order = $_GET[sort_by]; // we give $order the $_GET[] value $orderURL = "&sort_by=".$order; // we save a bit of the url in the variable $orderURL } else { // else if the retrieved $_GET[] value does not match anything from the array, we want it to // have a default value, which is: $order = "postdate"; // default sort_by is postdate, so by default it will sort the results by postdate } // next up, we are checking the ‘flow’, does the user want the results to be from top to bottom // or vice versa? $sort = $_GET[flow]; // we are retrieving the &flow= value from the URL if ($_GET[flow] == "ASC") { // we check if the value is equal to ‘ASC’ if it is: $otherSort = "DESC"; // we tell the script that $otherSort is ‘DESC’ $sortURL = "&flow=DESC"; // and we, once again, save a bit of the url in a variable } else { // else if the $_GET[flow] is not ASC we do: $sort = "DESC"; // instead of ASC $sort is now ‘DESC’ $sortURL = "&flow=ASC"; // and the saved url should be ASC } // next up, we want to check if the users wants to see only results with a certain category! $filter = $_GET[filter]; // we once again assign a variable to the retrieved information from the URL $filterArray = array("PHP", "HTML", "Photoshop"); // we create another array containing our categories $filterURL = NULL; // the base state of $filterURL is NULL, we don’t want it to have any value if the user isn’t // filtering the results! if (isset($filter) && in_array($filter, $filterArray)) { // check with the isset() function to see if $filter is set and if $filter matches any of the results // from the $filterArray, if we have a match: $filterURL = "&filter=".$filter; // we save the url in a variable once more! $a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' AND category = '$filter' ORDER BY $order $sort"); // if we are filtering, we want to change the queries ‘where’ , by adding ‘AND // category = ‘$filter’ we are letting the filter do it’s work } else { // if there is no filter set we don’t want to do anything special with the query $a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' ORDER BY $order $sort"); // the normal query, note the where clause, we are only selecting tutorials that are // approved! } // we close the else statement // we are not done yet, we have yet to display the tutorials in a list, so here we go! print "<h3>All Tutorials</h3>\n"; // we display where we are with a header type 3 print "<a href=\"tutorial.php?act=List\" target=\"_self\">Change to Default order</a>\n"; // we print a link to the default settings of the List case, in case they want to reset their filter. print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\"> <tr> <th><a href=\"".$url.$filterURL."&sort_by=title".$sortURL."\" target=\"_self\">Titel</a></th> <th><a href=\"".$url.$filterURL."&sort_by=category".$sortURL."\" target=\"_self\">Category</a></th> <th><a href=\"".$url.$filterURL."&sort_by=difficulty".$sortURL."\" target=\"_self\">Difficulty</a></th> <th><a href=\"".$url.$filterURL."&sort_by=rating".$sortURL."\" target=\"_self\">Rating</a></th> <th><a href=\"".$url.$filterURL."&sort_by=views".$sortURL."\" target=\"_self\">Views</a></th> <th><a href=\"".$url.$filterURL."&sort_by=author".$sortURL."\" target=\"_self\">Author</a></th> <th><a href=\"".$url.$filterURL."&sort_by=postdate".$sortURL."\" target=\"_self\">Posted on</a></th> </tr>"; // we just printed a table with some Table headers including the links to sort the results. // you See, there we see the variables again that we defined above, every time a user click // eg on title, the links changes from eg DESC to ASC to change the sorting :) if (mysql_num_rows($a) == 0) { // we check if the number of results are 0, if they are print " <tr> <td colspan=\"7\">There are no tutorials to display!</td> </tr>\n"; // sorry, no tutorials :( } else { // if we do have tutorials! while ($b = mysql_fetch_object($a)) { // once again we use mysql_fetch_object to execute a query! print " <tr> <td><a href=\"tutorial.php?act=Show&tid=".$b->id."\" target=\"_self\">".$b->title."</a></td> <td><a href=\"".$url."&filter=".$b->category.$orderURL.$sortURL."\" target=\"_self\">".$b->category."</a></td> <td>".$b->difficulty."</td> <td>".$b->rating."</td> <td>".$b->views."</td> <td>".$b->author."</td> <td>".$b->postdate."</td> </tr>\n"; // we print out all the tutorials on a new <tr>, the first <td> has a link // leading to the actual tutorial, that will be displayed in the Show case // the second <td> allows the user to filter by category! } } print "</table>\n"; // we close some brackets and end the table! ?> That much for the List case, I hope it wasn’t to hard and that your boat is still floating . Let’s end this part with the ‘Add’ case. This won’t be as hard as the List case, thank god <?PHP print "<h3>Add Tutorial</h3>\n"; // once more we display where we are! print "Please note that the tutorials you are submitting here, will be reviewed by the staff first. We reserve the right to decline/edit or remove your tutorial from our database at any time.\n"; // we give the user some information before they submit the tutorial if (!isset($_POST[posttut])) { // we check using !isset() to see if the submit button has been pressed, if it isn’t: print "<form method=\"post\">\n"; print "<strong>Title:</strong> \n<br />\n"; print "<input type=\"text\" name=\"title\"> \n<br />\n"; print "<strong>Subtitle:</strong> \n<br />\n"; print "<input type=\"text\" name=\"subtitle\"> \n<br />\n"; print "<strong>Author:</strong> \n<br />\n"; print "<input type=\"text\" name=\"author\"> \n<br />\n"; print "<strong>Category:</strong> \n<br />\n"; print "<select name=\"category\"> <option title=\"PHP\">PHP</option> <option title=\"HTML\">HTML</option> <option title=\"Photoshop\">Photoshop</option> </select>\n<br />\n"; print "<strong>Difficulty:</strong> \n<br />\n"; print "<select name=\"difficulty\"> <option title=\"Easy\">Easy</option> <option title=\"Medium\">Medium</option> <option title=\"Hard\">Hard</option> </select>\n<br />\n"; print "<strong>Content:</strong> <br />\n"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> \n<br />\n"; print "<input type=\"submit\" name=\"posttut\" value=\"Add Tutorial\">\n"; print "</form>\n"; // we display a simple form, containing all the values that are editable by the author. } else { // else if the submit button HAS been pressed: $getTitle = $_POST[title]; $getSubTitle = $_POST[subtitle]; $getAuthor = $_POST[author]; $getCategory = $_POST[category]; $getDifficulty = $_POST[difficulty]; $getContent = $_POST[content]; $getDate = date("Y-M-j"); $getApprove = 0; // we assign various variables to the posted content we retrieved from the form $insertTutorial = mysql_query("INSERT INTO tutorials (title, subtitle, author, difficulty, category, postdate, approve, content) VALUES ('$getTitle', '$getSubTitle', '$getAuthor', '$getDifficulty', '$getCategory', '$getDate', '$getApprove', '$getContent')"); // and we use the information from the form, and make a new record in our database // containing the new tutorial, since $getApprove is still 0, the tutorial is still awaiting // to be approved by anyone from the admin panel! print "Thank you, your tutorial has been added to our database and is awaiting approval by our staff. You wil either get a e-mail or a private message telling you if the tutorial has been approved/declined and/or modified!"; } // we close a bracket and the add part is done, easy huh, ?> Now all that remains is part3, I’ll post as soon as I can, but it’s time for me to get a beer and clear my head, see you all in part 3, once again if you have any questions/suggestions about this tutorial, feel free to ask it in the comments. Wildo
  18. Tutorial CMS – Part 1 So I was writing a tutorial CMS for learning purposes the other day, and I got convinced to turn it into a tutorial. So here it is! I divided the tutorial in 3 parts, below you can see what each part will contain. To make this work you will need a PHP enabled server with a mysql database and of course any text editor of your choice! This tutorial includes: Part1: Latest and Popular tutorials Part2: All tutorials with various sorting options, like only certain category, sort by title, asc/desc you name it! Part 1: Show single tutorial Part 2: Add tutorial with approve/decline by staff Part 3:Add(without approval)/Edit and Delete admin panel All tutorials will have a lot of rows. Like : Title Subtitle Author Category Difficulty Views Rating Votes Access Attachment (non-upload able) And approve This tutorial will be a all-in-one-file-script, meaning that we will only be creating one file to make it all happen, you can of course edit the script to suit your needs with several pages, like a universal config.php file, which most of you will have. Oh btw, Don’t worry, I will paste the full working code at the end of this tutorial And here is the mysql table you need: CREATE TABLE `tutorials` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `title` TINYTEXT NOT NULL , `subtitle` TINYTEXT NOT NULL , `category` TINYTEXT NOT NULL , `author` TINYTEXT NOT NULL , `rating` VARCHAR( 255 ) NOT NULL , `approve` VARCHAR( 255 ) NOT NULL , `postdate` DATE NOT NULL , `acces` VARCHAR( 255 ) NOT NULL , `views` INT( 11 ) NOT NULL , `content` LONGTEXT NOT NULL , `attachement` VARCHAR( 255 ) NOT NULL , `difficulty` VARCHAR( 255 ) NOT NULL , `votes` VARCHAR( 255 ) NOT NULL ) ENGINE = MYISAM; So, you think we are ready for some php? I think we are . . . so let’s go! We start off by creating a function and connecting to our database. I commented all the lines I thought were important. <?PHP function Ratethis($rating) { // here we are creating a function called 'Ratethis', // we will need this function to calculate the rating of a tutorial! if ((($rating >= 1.00)or($rating == 1.50)) && ($rating <= 1.49)) { return '1'; } // ok this comment counts for every if statement in this function // we check if the rating given in the string $rating is within certain values // to calculate wich value we want to return. so let's say $rating is '1.88' it // would return 1.5 as below. giving the tutorial the rating of 1.5 // the reason I do this, is to avoid ratings like 1.43576 // that just looks sloppy don't you think? if ((($rating >= 1.50)or($rating == 1.50)) && ($rating <= 1.99)) { return '1.5'; } if ((($rating >= 2.00)or($rating == 2.00)) && ($rating <= 2.49)) { return '2'; } if ((($rating >= 2.50)or($rating == 2.50)) && ($rating <= 2.99)) { return '2.5'; } if ((($rating >= 3.00)or($rating == 3.00)) && ($rating <= 3.49)) { return '3'; } if ((($rating >= 3.50)or($rating == 3.50)) && ($rating <= 3.99)) { return '3.5'; } if ((($rating >= 4.00)or($rating == 4.00)) && ($rating <= 4.49)) { return '4'; } if ((($rating >= 4.50)or($rating == 4.50)) && ($rating <= 4.99)) { return '4.5'; } if ($rating == 5.0) { return '5'; } // and we close the function called // Ratethis with a lovely bracket! } $connect = mysql_connect("localhost", "root", ""); // let's connect to our oh so lovely database! $cdb = mysql_select_db("test"); // and let's select a database as wel;) quiet important :P ?> I hope that wasn’t to hard... we continue by adding a ‘framework’ to our system. Take a look, I commented it as before. <?PHP // so like any other system, we want links to the different sections within the system! // so let's make some! print "<h2>Tutorials</h2>\n"; print "<a href=\"tutorial.php?act=\" target=\"_self\">Tutorial Index</a>"; print " | \n"; print "<a href=\"tutorial.php?act=List\" target=\"_self\">All Tutorials</a>"; print " | \n"; print "<a href=\"tutorial.php?act=Add\" target=\"_self\">Add a Tutorial</a>"; print " | \n"; print "<a href=\"tutorial.php?act=Admin\" target=\"_self\">Admin</a>\n"; print "<br /><br />\n"; // hmm. . . those links don't work yet! let's make them work by // creating this 'framework' for this system switch ($_GET[act]) { // using switch ($var) { we create some snazzy and functional links // it will make the link look like tutorial.php?act= snazzy huh;) default: // the default page we want to display if $_GET[act] isn’t defined in the URL break; case ‘Show’: // if the link is tutorial.php?act=Show we display what is inside this and the break; line break; case ‘List’: // if the link is tutorial.php?act=List we display what is inside this and the break; line break; case ‘Add : // if the link is tutorial.php?act=Add we display what is inside this and the break; line break; case ‘Admin’: // if the link is tutorial.php?act=Admin we display what is inside this and the break; line break; } // remember we opened brackets when we used the switch statement? Now we close them mysql_close($connect); // and we close our connect variable, not really important, don’t really know why I’m doing // this but Hey . . who cares :p ?> Ok, so we have our framework set up. Time for some real hard coding! I’m going to work down on our framework starting with the case ‘Default’ and ending with the case ‘Admin’. Bare with me . Note: we are now coding inside default: and the first break;! Basically what I, excuse me, We want to display, is the latest and the most popular tutorials in our database. We will start with the latest. <?PHP print "<h3>Latest Tutorials</h3>\n"; // we print a header telling the visitor what it will see below $a = mysql_query("SELECT * FROM tutorials WHERE approve = '1' ORDER BY postdate DESC LIMIT 5"); // above is the query selecting results where there approve row is set to 1, we order those // results by postdate and descending, meaning from top to bottom and limiting the results to 5 print "<a href=\"tutorial.php?act=List\" target=\"_self\">All Tutorials</a>\n"; // we print a link to all the tutorials, this is in the List case which we will make soon! print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\"> <tr> <th>Titel</th> <th>Category</th> <th>Difficulty</th> <th>Rating</th> <th>Views</th> <th>Author</th> <th>Posted on</th> </tr>"; // above we printed a table to contain our results we will display some lines below. if (mysql_num_rows($a) == 0) { // if the amount of results retrieved, using mysql_num_rows is equal to 0 we do this: print " <tr> <td colspan=\"7\">There are no tutorials to display!</td> </tr>\n"; // we print that there are no tutorials :( } else { // if the amount of results retrieved is NOT equal to 0 we do this: while ($b = mysql_fetch_object($a)) { // using a while loop with mysql_fetch_object, wich will make it so // that all the results are treated like a object print " <tr> <td><a href=\"tutorial.php?act=Show&tid=".$b->id."\" target=\"_self\">".$b->title."</a></td> <td>".$b->category."</td> <td>".$b->difficulty."</td> <td>".$b->rating."</td> <td>".$b->views."</td> <td>".$b->author."</td> <td>".$b->postdate."</td> </tr>\n"; // we print every results, you see that the first results even has a link, // the link brings the visitor to the full tutorial } // we close the while loop } // we close the else statement print "</table>\n"; // we end the table ?> Well that was a ****load of code wasn’t it, I hope it wasn’t to hard and that I commented it enough. If not, you’re out of luck . So we have on case finished, let’s continue with the next case, ‘Show’! Show will display the full tutorial: <?PHP print "<h3>Show Tutorial</h3>\n"; // once again we show were we are with a header 3 $tid = $_GET[tid]; // we are assigning a variable to the $_GET[id] for further reference, if you don’t understand // what $_GET[id] does, it looks inside the URL for a piece of text like &id=(SOMETHING) if (!isset($tid) || !is_numeric($tid)) { // here we check using a ‘!’ to see if the $tid variable is NOT set or that the $tid variable is not // numeric. If it is not set or it isn’t numeric we do what is inside the brackets print "Invalid or no ID given, redirecting you. . .\n"; // we tell the users that the id they have provided is invalid print "<meta http-equiv=\"refresh\" content=\"3;URL=tutorial.php?act=List\" />\n"; // and we redirect the user to the List case to see all the tutorials! } else { // if the $tid variable IS set and has a numeric value we do what is inside these brackets! $a = mysql_query("SELECT * FROM tutorials WHERE id = '$tid' AND approve = '1' LIMIT 1"); // we make a query select 1 tutorials, see the limit1?, where the id is corresponding to // the value of the $tid variable! if (mysql_num_rows($a) == 0) { // now we check if got any results from that query, if the number of results is equal to // 0, we, once again, do what is inside these brackets print "The tutorial matching the id you provided does not exist!"; // sorry no tutorials match that id! } else { // else if the results is greater then 1 we do this: while ($b = mysql_fetch_object($a)) { // once again we create a while loop with mysql_fetch_object(); $newView = $b->views + 1; // we add a view by grabbing the old amount of views // retrieved from the query and adding 1 to it. $addView = mysql_query("UPDATE tutorials SET views = '$newView' WHERE id = '$tid'"); // and we update the amount of views with the new amount of views! $attachement = $b->attachement; // we assign a new variable to the attachement object retrieved from the // query if (!$attachement) { // we check if $attachement has any value, if it doesn’t we do this: $attachement = "--"; // gives $attachement a new value, } else { // $attachment does have a value! We do this: $attachement = "<a href=\"".$b->attachement."\" target=\"blank\">Click to download</a>"; // $attachement is now a link with the URL being the attachement // object! } if (isset($_POST[ratethis])) { // we check if the submit button for the rating has been pressed! // I’m not 100% sure this actually works as intended, // it’s up to you to figure it out, I’m no genius at math so… :( $newVotes = $b->votes + 1; $old_total = $b->rating * $b->votes; $new_total = $old_total + $_POST[rating]; $new_rating = $new_total / $newVotes; $setNewRating = Ratethis($new_rating); $updateRating = mysql_query("UPDATE tutorials SET votes = '$newVotes', rating = '$setNewRating' WHERE id = '$b->id'"); // we update the new rating for the tutorial displayed $rateIt = "Thanks for the vote!"; // we make a variable containing a message } else { // if the submit button for the rating hasn’t been pressed $rateIt = "<select name=\"rating\"> <option value=\"5\" selected>5</option> <option value=\"4\">4</option> <option value=\"3\">3</option> <option value=\"2\">2</option> <option value=\"1\">1</option> </select> <input type=\"submit\" name=\"ratethis\" value=\"Rate\">"; // $rateIt got a new value, it is now the dropdownbox for the // rating waiting to be displayer } print "<form method=\"post\">"; // we print some html, starting a form! print "<strong>".$b->title."</strong>\n"; // we print out the tutorial’s title in bold text print "<hr noshade color=\"#222222\" size=\"1px\" />\n"; // we make a nice horizontal line across the screen print "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"10\"> // we print some html, containing the html for a table <tr> <td width=\"40%\" valign=\"top\">Category: <strong>".$b->category."</strong><br /> Difficulty: <strong>".$b->difficulty."</strong><br /> Rating: <strong>".$b->rating."</strong> (".$b->votes." votes) ".$rateIt."<br /> Views: <strong>".$b->views."</strong><br /></td> <td valign=\"top\">Posted on: <strong>".$b->postdate."</strong><br /> Acces: <strong>".$b->acces."</strong><br /> Author: <strong>".$b->author."</strong><br /> Attachement: <strong>".$attachement."</strong><br /></td> </tr> </table>\n"; // that is just some basic html/php to show various information from the information retrieved // from the database print "<hr noshade color=\"#222222\" size=\"1px\" />\n"; // we make another nice little line print nl2br($b->content)."\n"; // we print the actual tutorial, with the n12br() function to allow // $b->content to have linebreaks and stuff! print "</form>\n"; // we close the form } } } // and we close some brackets! ?> That’s it for part 1, at the end of part 3 you will find the whole working code, Any questions/suggestions Let me know Wildo
  19. When I want to create image rollovers most of the time I use Dreamweavers method, I like the option to pre-load the hover images Wildo
  20. could be shorter, wrote it ages ago, litteraly I think I used echo "message": exit (); while I could have done exit ("message"); little shorter Wildo
  21. News CMS Well hello there, I thought it was time for another tutorial, this time I'll 'try' to teach y'all how to write a 'decent' news cms! so let's get started shall we? Contains: Latest News Full article view Archive with fancy pagination Admin panel including add, edit and delete functions Requires: a server with PHP enabled a MySQL database notepad NOTE: if you intent to use this code on your website, take caution, this news cms comes with no security, so if you intend to use it, add a decent security to it, this script is NOT, I repeat not secure in any way! Now we got that sorted out, let's finally start... first of all, we need a MySQL table to let this work, so I'll just give you the code and just assume you know how to create it and stuff...! CREATE TABLE `SW_News` ( `id` int(11) NOT NULL auto_increment, `author` varchar(25) NOT NULL, `title` varchar(255) NOT NULL default '', `category` varchar(100) NOT NULL, `date` varchar(20) NOT NULL, `content` TEXT NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; Ok, I think we are ready for some php . Let’s do it! Open up your notepad, dreamweaver or whatever you’re using! First of all, we need to connect to our database… so let’s do that, shall we? <?PHP $host = "****"; $user = "****"; $pass = "****"; $db = "****"; $connect = mysql_connect($host, $user, $pass); mysql_select_db($db, $connect); ?> That’s just easy connecting baby, note that I didn’t use or die (mysql_error()); simply because I already tested the whole code and there were no errors, if you’re reading to learn from this, which I hope you are, I advise you to use or die (mysql_error()); after each query so you’re sure there are no errors there! Now we are going to display 3 links linking to the 3 sections (there are 4 sections, but 1 section doesn’t require a link), the link URL might confuse you now, but you’ll understand it ( hopefully ) after this tutorial . <?PHP print "<a href=\"index.php\" target=\"_self\">News Index</a> | "; print "<a href=\"index.php?v=nar\" target=\"_self\">News Archive</a> | "; print "<a href=\"index.php?v=aa\" target=\"_self\">News Admin</a> "; print "<br /><br />"; ?> The next thing we are going to do, is to make some fancy links, YAY, besides the INSANE COOL look, it also adds some functionality to our script . <?PHP switch ($_GET[v]) { default: ?> That piece is simple, we are ‘switching’ to the part displayed in the link after ‘?v=’, and if there is no ‘v’ set, the script will show the default part, which we are going to create now. First things first, what do we want to display in the default part? Right, you’re a smart boy/girl, the latest news! Woohooo! <?PHP print "<strong>News Index:</strong>"; print "<br /><br />"; $sql = "SELECT * FROM SW_News ORDER BY date DESC LIMIT 3"; $rs = mysql_query($sql, $connect); if(mysql_num_rows($rs) > 0) { while($row = mysql_fetch_object($rs)) { $content = nl2br($row->content); if (strlen($content) > 750) { $content = substr ("$content", 0, 500); $content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>"; } else { $content .= ""; } print "<div>"; print "<b><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">".$row->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $row->author." | ".$row->category." | ".$row->date; print "</div>"; print "<hr color=\"#CCCCCC\" size=\"1px\">"; print "</div>"; } } else { print "Sorry, no results found."; } break; ?> Ok that was a load of code wasn’t it, let’s explain what I’m doing! We started of by printing a bold text displaying where we are! The ‘News Index’! then we created a query selecting the latest 3 records from our SW_News table, you can change the LIMIT 3 to whatever floats your boat, customizing for the win baby! Then we checked if we the result of our query was greater then 0, because if we don’t have any records to display, we’ll have a problem, so with the else statement I printed if no results, ‘sorry, no results found!’. Neat huh! I used mysql_fetch_object instead of mysql_fetch_array, the only difference is that the MySQL table results are treated as a object instead of putted in a array, no biggie baby! <?PHP $content = nl2br($row->content); if (strlen($content) > 750) { $content = substr ("$content", 0, 500); $content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>"; } else { $content .= ""; } ?> Ok that might confused you. But it’s not that difficult. Using the n12br() function, we allow line breaks in our $content variable. After that it gets tricky! Here’s the final code, for the copy+pasters amongst us. Using strlen() we check if the string, in this case $content, is longer that 750 characters, if it is, we use substr() to select only the first 500 characters to avoid our news index to get to lengthy, after that we use ‘.=’ to ‘add’ something to the $content variable. We add ‘… AND A LINK TO THE FULL ARTICLE’, if the $content variable is not longer then 750 characters we add nothing to our $content variable! Let’s continue! We now arrived at a new ‘case’, the ‘fa’ case, which is short for ‘Full article’! here’s some code to look at! <?PHP case 'fa'; print "<strong>Full Article:</strong>"; print "<br /><br />"; $getTheId = $_GET[id]; if (!$getTheId) { print "Please enter a article id to display!"; } else { if (!is_numeric($_GET[id])) { print "Sorry, invalid <strong>ID</strong> given!"; } else { $newsId = $_GET[id]; $getNews = "SELECT * FROM SW_News WHERE id = $newsId"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); print "<div>"; print "<b>".$displayNews->title."</b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } } break; ?> We are doing the same thing at the beginning here. We display in bold text where we are, then we are going to get some information from our url, the ‘id’ part, so if our url looked like index.php?v=fa&id=123 we’d be looking at id=123. Then we check if there is any information at all in the ‘id’ if not, we display an error, then we check if the ‘id’ is at least a number, if it isn’t, we display a error again, if it is, we continue! Then we create a query, select the news article with the corresponding id, and we display it, the display part is fully editable as long as you don’t touch the variables! YAY, we finished another case! Next up is the News archive. This will be a big chunk of code, so enjoy, I’ll explain it afterwards! <?PHP case 'nar': print "<strong>News Archive:</strong>"; $pageNumber = (isset($_GET["p"]) && is_numeric($_GET["p"])) ? $_GET["p"] : 1; $perPage = 10; $padding = 3; $startIndex = ($pageNumber * $perPage) - $perPage; $totalCount = "SELECT COUNT(*) as 'Total' FROM SW_News"; $rsCount = mysql_query($totalCount, $connect); $rowCount = mysql_fetch_object($rsCount); $numOfPages = ceil($rowCount->Total / $perPage); print "<div align=\"center\">"; print "<a href=\"index.php?v=nar&p=1\">«</a> "; if(($pageNumber - $padding) > 1) { print "... "; $lowerLimit = $pageNumber - $padding; for($i = $lowerLimit; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } else { for($i = 2; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) { print "<b> - " . $pageNumber . " - </b>"; } if(($pageNumber + $padding) < $numOfPages) { $upperLimit = $pageNumber + $padding; for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } print "... "; } else { for($i = ($pageNumber + 1); $i < $numOfPages; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>"; print "</div>"; $getAllNews = "SELECT * FROM SW_News ORDER BY date DESC LIMIT $startIndex, $perPage"; $newsAllInbound = mysql_query($getAllNews, $connect); while ($displayAllNews = mysql_fetch_object($newsAllInbound)) { print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>"; print "<br />"; print $displayAllNews->author." | ".$displayAllNews->category." | ".$displayAllNews->date; print "<br /><br />"; } print "<div align=\"center\">"; print "<a href=\"index.php?v=nar&p=1\">«</a> "; if(($pageNumber - $padding) > 1) { print "... "; $lowerLimit = $pageNumber - $padding; for($i = $lowerLimit; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } else { for($i = 2; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) { print "<b> - " . $pageNumber . " - </b>"; } if(($pageNumber + $padding) < $numOfPages) { $upperLimit = $pageNumber + $padding; for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } print "... "; } else { for($i = ($pageNumber + 1); $i < $numOfPages; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>"; print "</div>"; break; ?> Ok well, we start as usual, we display where we are , then we start our navigation! NOTE: I didn’t write the navigation code myself, I was being lazy and I used the code found on www.sampsonresume.com to add pagination to the archive, so the credit for the pagination goes to ‘sampson’, the pagination is a bit hard to explain in text, so I suggest you take a look at the video tutorial that ‘sampson’ wrote on his website, this is the link http://www.sampsonresume.com/2007/02/01/sc...php-pagination/ . one thing left about this case, you should take a look at <?PHP print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>"; ?> We are linking to the full article case there, to view that article, so all the cases are ‘connected’ so to say with each other! Buh, I’m tired of writing already, and the hardest part is incoming as I’m writing… the ADMIN panel… WOOoooHOoooo! If you’re already lost, don’t worry, I’ll post the whole WORKING code at the bottom of this tutorial. Oh boy, here we go, let’s start a new case! <?PHP case 'ad': $adminPage = $_GET[aa]; if (!$adminPage) { print "<strong>News Admin:</strong>"; print "<br /><br />"; print "<a href=\"index.php?v=ad&aa=add\" target=\"_self\">Add News</a> <br />"; print "<a href=\"index.php?v=ad&aa=edit\" target=\"_self\">Edit News</a> <br />"; print "<a href=\"index.php?v=ad&aa=del\" target=\"_self\">Delete News</a>"; } ?> We begin, as usual, with our bolded text where we are, then we check if ‘aa’ (short for admin action) was inserted in the url, and if it was not, we display our admin index which contains 3 links to the 3 sections inside the admin panel, add, edit and delete. Let’s continue. <?PHP if ($adminPage == "add") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $checkButton = $_POST[addnews]; if (!$checkButton) { print "<form method=\"post\">"; print "<strong>Title:</strong> <br />"; print "<input type=\"text\" name=\"title\"> <br />"; print "<strong>Author:</strong> <br />"; print "<input type=\"text\" name=\"author\"> <br />"; print "<strong>Category:</strong> <br />"; print "<select name=\"category\"> <option title=\"yourcategory0\">Yourcategory0</option> <option title=\"yourcategory1\">Yourcategory1</option> <option title=\"yourcategory2\">Yourcategory2</option> <option title=\"yourcategory3\">Yourcategory3</option> <option title=\"yourcategory4\">Yourcategory4</option> </select><br />"; print "<strong>Content:</strong> <br />"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> <br />"; print "<input type=\"submit\" name=\"addnews\" value=\"Add News\">"; print "</form>"; } else { $username = $_POST[author]; $title = $_POST[title]; $category = $_POST[category]; $content = $_POST[content]; if (!$username || !$title || !$category || !$content) { print "You forgot a field! Please go back and fill all the fields!"; } else { $ndate = date('H:i d-m-Y'); $addingNews = "INSERT INTO SW_News (author, title, category, date, content) VALUES('$username', '$title', '$category', '$ndate', '$content')"; $processNew = mysql_query($addingNews, $connect); print "News item added successfully!"; } } } ?> Well that’s the code to add news to our database, let’s break it down shall we!, first we check if the ‘aa’ in the link says ‘ add’ and if it does we start, we check if the form has been submitted, if it isn’t, we display the form, if it is, we process the form. I won’t explain that part to good, I don’t think it’s that hard php, but if you really don’t get what I’m doing here, feel free to ask, I’m always in to help people! Let’s get going with the edit part! <?PHP if ($adminPage == "edit") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $editId = $_GET[ei]; if (!$editId || !is_numeric($editId)) { $getNews = "SELECT * FROM SW_News ORDER BY date DESC"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); print "<div>"; print "<b><a href=\"index.php?v=ad&aa=edit&ei=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } else { if (!$_POST[editnews]) { $getTheIdNews = "SELECT * FROM SW_News WHERE id = $editId"; $newsInbound1 = mysql_query($getTheIdNews, $connect); while ($displayEditNews = mysql_fetch_object($newsInbound1)) { print "<form method=\"post\">"; print "<strong>Title:</strong> <br />"; print "<input type=\"text\" value=\"".$displayEditNews->title."\" name=\"title\"> <br />"; print "<strong>Author:</strong> <br />"; print "<input type=\"text\" value=\"".$displayEditNews->author."\" name=\"author\"> <br />"; print "<strong>Category:</strong> <br />"; print "<select name=\"category\"> <option title=\"yourcategory\" selected>Yourcategory</option> <option title=\"yourcategory1\">Yourcategory1</option> <option title=\"yourcategory2\">Yourcategory2</option> <option title=\"yourcategory3\">Yourcategory3</option> <option title=\"yourcategory4\">Yourcategory4</option> </select><br />"; print "<input type=\"hidden\" name=\"newsid\" value=\"".$displayEditNews->id."\">"; print "<strong>Content:</strong> <br />"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$displayEditNews->content."</textarea> <br />"; print "<input type=\"submit\" name=\"editnews\" value=\"Edit News\">"; print "</form>"; } } else { $title = $_POST[title]; $author = $_POST[author]; $category = $_POST[category]; $content = $_POST[content]; $editIdNews = $_POST[newsid]; $updateTheNews = "UPDATE SW_News SET title = '$title', author = '$author', category = '$category', content = '$content' WHERE id = $editIdNews"; $fini****Up = mysql_query($updateTheNews, $connect); print "The article <strong>".$title."</strong> has successfully been edited!"; } } } ?> First of all, we need to grab an id from a list to know which article we’ll be updating, so we first display a list of all the news articles, then if you click on one of them, we retrieve the id of the corresponding article and we select the data from that article! We then display all those data in a form, so that is it ready for editing. Once you pressed the submit button, we do the same as in the add case only this time we UPDATE instead of INSERT. Easy huh? Anyways here comes the delete part, <?PHP if ($adminPage == "del") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $delId = $_GET[di]; if (!$delId || !is_numeric($delId)) { $getNews = "SELECT * FROM SW_News ORDER BY date DESC"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); if (strlen($content) > 750) { $content = substr ("$content", 0, 500); $content .= " ..."; } else { $content .= ""; } print "<div>"; print "<b><a href=\"index.php?v=ad&aa=del&di=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } else { if (!$_POST[delnews]) { $getTheIdNews = "SELECT * FROM SW_News WHERE id = $delId"; $newsInbound1 = mysql_query($getTheIdNews, $connect); while ($displayDelTitle = mysql_fetch_object($newsInbound1)) { print "Are you sure you want to delete the article <strong>".$displayDelTitle->title."</strong>?"; print "<form method=\"post\">"; print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">"; print "</form>"; } } else { $updateTheNews = "DELETE FROM SW_News WHERE id = '$delId'"; $fini****Up = mysql_query($updateTheNews, $connect); print "The article <strong>".$title."</strong> has succesfully been deleted!"; } } } break; ?> The delete part is actually quiet easy, we do the same as in the edit case, we create a list of all the articles with clickable titles, if you click on a news title, you will get a question ‘ are you sure you want to delete’ then if you are sure it deletes the article from our table. That’s it, oh wait, we forgot to close some things! <?PHP } mysql_close($connect); ?> We just closed the ‘switch’ statement from the top of the page, and we closed our mysql connection, and we’re done, I hope you learned something, if you encounter any problems or just have a question, feel free to comment below. I realize I didn’t explain all there is to it what I’ve just shown you, but if you want me to explain it more thoroughly, feel free to ask! Here’s the full code btw: <?php $host = "****"; $user = "****"; $pass = "****"; $db = "****"; $connect = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db, $connect) or die(mysql_error()); print "<a href=\"index.php\" target=\"_self\">News Index</a> | "; print "<a href=\"index.php?v=nar\" target=\"_self\">News Archive</a> | "; print "<a href=\"index.php?v=ad\" target=\"_self\">News Admin</a> "; print "<br /><br />"; switch ($_GET[v]) { default: print "<strong>News Index:</strong>"; print "<br /><br />"; $sql = "SELECT * FROM SW_News ORDER BY date DESC LIMIT 3"; $rs = mysql_query($sql, $connect); if(mysql_num_rows($rs) > 0) { while($row = mysql_fetch_object($rs)) { $content = nl2br($row->content); if (strlen($content) > 750) { $content = substr ("$content", 0 , 500); $content .= "...<br /><div align=\"center\"><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">Full article</a></div>"; } else { $content .= ""; } print "<div>"; print "<b><a href=\"index.php?v=fa&id=".$row->id."\" target=\"_self\">".$row->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $row->author." | ".$row->category." | ".$row->date; print "</div>"; print "<hr color=\"#CCCCCC\" size=\"1px\">"; print "</div>"; } } else { print "Sorry, no results found."; } break; case 'fa'; print "<strong>Full Article:</strong>"; print "<br /><br />"; $getTheId = $_GET[id]; if (!$getTheId) { print "Please enter a article id to display!"; } else { if (!is_numeric($_GET[id])) { print "Sorry, invalid <strong>ID</strong> given!"; } else { $newsId = $_GET[id]; $getNews = "SELECT * FROM SW_News WHERE id = $newsId"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); print "<div>"; print "<b>".$displayNews->title."</b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } } break; case 'nar': print "<strong>News Archive:</strong>"; $pageNumber = (isset($_GET["p"]) && is_numeric($_GET["p"])) ? $_GET["p"] : 1; $perPage = 10; $padding = 3; $startIndex = ($pageNumber * $perPage) - $perPage; $totalCount = "SELECT COUNT(*) as 'Total' FROM SW_News"; $rsCount = mysql_query($totalCount, $connect); $rowCount = mysql_fetch_object($rsCount); $numOfPages = ceil($rowCount->Total / $perPage); print "<div align=\"center\">"; print "<a href=\"index.php?v=nar&p=1\">«</a> "; if(($pageNumber - $padding) > 1) { print "... "; $lowerLimit = $pageNumber - $padding; for($i = $lowerLimit; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } else { for($i = 2; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) { print "<b> - " . $pageNumber . " - </b>"; } if(($pageNumber + $padding) < $numOfPages) { $upperLimit = $pageNumber + $padding; for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } print "... "; } else { for($i = ($pageNumber + 1); $i < $numOfPages; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>"; print "</div>"; $getAllNews = "SELECT * FROM SW_News ORDER BY date DESC LIMIT $startIndex, $perPage"; $newsAllInbound = mysql_query($getAllNews, $connect); while ($displayAllNews = mysql_fetch_object($newsAllInbound)) { print "<b><a href=\"index.php?v=fa&id=".$displayAllNews->id."\" target=\"_self\">".$displayAllNews->title."</a></b>"; print "<br />"; print $displayAllNews->author." | ".$displayAllNews->category." | ".$displayAllNews->date; print "<br /><br />"; } print "<div align=\"center\">"; print "<a href=\"index.php?v=nar&p=1\">«</a> "; if(($pageNumber - $padding) > 1) { print "... "; $lowerLimit = $pageNumber - $padding; for($i = $lowerLimit; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } else { for($i = 2; $i < $pageNumber; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } if(($pageNumber != 0) && ($pageNumber != 1) && ($pageNumber != $numOfPages)) { print "<b> - " . $pageNumber . " - </b>"; } if(($pageNumber + $padding) < $numOfPages) { $upperLimit = $pageNumber + $padding; for($i = ($pageNumber + 1); $i <= $upperLimit; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } print "... "; } else { for($i = ($pageNumber + 1); $i < $numOfPages; $i++) { print "<a href=\"index.php?v=nar&p=".$i."\">".$i."</a> "; } } print "<a href=\"index.php?v=nar&p=".$numOfPages."\">»</a>"; print "</div>"; break; case 'ad': $adminPage = $_GET[aa]; if (!$adminPage) { print "<strong>News Admin:</strong>"; print "<br /><br />"; print "<a href=\"index.php?v=ad&aa=add\" target=\"_self\">Add News</a> <br />"; print "<a href=\"index.php?v=ad&aa=edit\" target=\"_self\">Edit News</a> <br />"; print "<a href=\"index.php?v=ad&aa=del\" target=\"_self\">Delete News</a>"; } if ($adminPage == "add") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $checkButton = $_POST[addnews]; if (!$checkButton) { print "<form method=\"post\">"; print "<strong>Title:</strong> <br />"; print "<input type=\"text\" name=\"title\"> <br />"; print "<strong>Author:</strong> <br />"; print "<input type=\"text\" name=\"author\"> <br />"; print "<strong>Category:</strong> <br />"; print "<select name=\"category\"> <option title=\"yourcategory\">Yourcategory</option> <option title=\"yourcategory1\">Yourcategory1</option> <option title=\"yourcategory2\">Yourcategory2</option> <option title=\"yourcategory3\">Yourcategory3</option> <option title=\"yourcategory4\">Yourcategory4</option> </select><br />"; print "<strong>Content:</strong> <br />"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\"></textarea> <br />"; print "<input type=\"submit\" name=\"addnews\" value=\"Add News\">"; print "</form>"; } else { $username = $_POST[author]; $title = $_POST[title]; $category = $_POST[category]; $content = $_POST[content]; if (!$username || !$title || !$category || !$content) { print "You forgot a field! Please go back and fill all the fields!"; } else { $ndate = date('H:i d-m-Y'); $addingNews = "INSERT INTO SW_News (author, title, category, date, content) VALUES('$username', '$title', '$category', '$ndate', '$content')"; $processNew = mysql_query($addingNews, $connect); print "News item added succesfully!"; } } } if ($adminPage == "edit") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $editId = $_GET[ei]; if (!$editId || !is_numeric($editId)) { $getNews = "SELECT * FROM SW_News ORDER BY date DESC"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); print "<div>"; print "<b><a href=\"index.php?v=ad&aa=edit&ei=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } else { if (!$_POST[editnews]) { $getTheIdNews = "SELECT * FROM SW_News WHERE id = $editId"; $newsInbound1 = mysql_query($getTheIdNews, $connect); while ($displayEditNews = mysql_fetch_object($newsInbound1)) { print "<form method=\"post\">"; print "<strong>Title:</strong> <br />"; print "<input type=\"text\" value=\"".$displayEditNews->title."\" name=\"title\"> <br />"; print "<strong>Author:</strong> <br />"; print "<input type=\"text\" value=\"".$displayEditNews->author."\" name=\"author\"> <br />"; print "<strong>Category:</strong> <br />"; print "<select name=\"category\"> <option title=\"yourcategory\" selected>Yourcategory</option> <option title=\"yourcategory1\">Yourcategory1</option> <option title=\"yourcategory2\">Yourcategory2</option> <option title=\"yourcategory3\">Yourcategory3</option> <option title=\"yourcategory4\">Yourcategory4</option> </select><br />"; print "<input type=\"hidden\" name=\"newsid\" value=\"".$displayEditNews->id."\">"; print "<strong>Content:</strong> <br />"; print "<textarea name=\"content\" cols=\"60\" rows=\"20\">".$displayEditNews->content."</textarea> <br />"; print "<input type=\"submit\" name=\"editnews\" value=\"Edit News\">"; print "</form>"; } } else { $title = $_POST[title]; $author = $_POST[author]; $category = $_POST[category]; $content = $_POST[content]; $editIdNews = $_POST[newsid]; $updateTheNews = "UPDATE SW_News SET title = '$title', author = '$author', category = '$category', content = '$content' WHERE id = $editIdNews"; $fini****Up = mysql_query($updateTheNews, $connect); print "The article <strong>".$title."</strong> has succesfully been edited!"; } } } if ($adminPage == "del") { print "<strong>News Admin:</strong>"; print "<br /><br />"; $delId = $_GET[di]; if (!$delId || !is_numeric($delId)) { $getNews = "SELECT * FROM SW_News ORDER BY date DESC"; $newsInbound = mysql_query($getNews, $connect); while ($displayNews = mysql_fetch_object($newsInbound)) { $content = nl2br($displayNews->content); if (strlen($content) > 750) { $content = substr ("$content", 0, 500); $content .= " ..."; } else { $content .= ""; } print "<div>"; print "<b><a href=\"index.php?v=ad&aa=del&di=".$displayNews->id."\" target=\"_self\">".$displayNews->title."</a></b><br />"; print $content; print "<div align=\"right\">"; print $displayNews->author." | ".$displayNews->category." | ".$displayNews->date; print "</div>"; print "</div>"; } } else { if (!$_POST[delnews]) { $getTheIdNews = "SELECT * FROM SW_News WHERE id = $delId"; $newsInbound1 = mysql_query($getTheIdNews, $connect); while ($displayDelTitle = mysql_fetch_object($newsInbound1)) { print "Are you sure you want to delete the article <strong>".$displayDelTitle->title."</strong>?"; print "<form method=\"post\">"; print "<input type=\"submit\" value=\"Delete!\" name=\"delnews\">"; print "</form>"; } } else { $updateTheNews = "DELETE FROM SW_News WHERE id = '$delId'"; $fini****Up = mysql_query($updateTheNews, $connect); print "The article <strong>".$title."</strong> has succesfully been deleted!"; } } } break; } mysql_close($connect); ?> not one of my best tutorials, but have fun with it. Wildo
  22. With this tutorial I'll show you people how you can easily protect your php files with just a couple lines of code! Here it goes: for example: you do not want people to be able to go directly to your config.php file. lets assume your config.php file looks like this: <?PHP $chost = "localhost"; $cusername = "YOURUSERNAME"; $cpassword = "YOURPASSWORD"; $cdb = "YOURDATABASE"; mysql_connect($chost, $cusername, $cpassword); mysql_select_db($cdb); ?> That is just a basic mysql connect script. now to protect it! We are adding 4 lines of code, take a look: <?PHP if(!defined("SESAMOPEN")){ echo "What are you doing here? You're not allowed to be here, be gone you pest!"; exit(); } $chost = "localhost"; $cusername = "YOURUSERNAME"; $cpassword = "YOURPASSWORD"; $cdb = "YOURDATABASE"; mysql_connect($chost, $cusername, $cpassword); mysql_select_db($cdb); ?> Ok, you may think, what in gods name is he doing? Well, hmm ok I understand, let's explain what I just did. <?PHP if(!defined("SESAMOPEN")){ ?> We are using a if statement to check if 'SESAMOPEN' has been defined, if it is not (see, we are using a '!' in front of defined) tell the user that they are lost and should go back. <?PHP echo "What are you doing here? You're not allowed to be here, be gone you pest!"; ?> and last using exit(); to stop and closing the if statement with a } <?PHP exit(); } ?> Now you think you're ready, but your not! we are only 50% done! Because, with that code SESAMOPEN will never be defined and thus your own script wont even be able to get access to config.php! So whenever you want to include config.php to your script to get mysql content you need to add this above the line where you include config.php: <?PHP define("SESAMOPEN", 1); ?> That line gives SESAMOPEN a value, so it is ‘defined’ and it will pass the if statement in your config.php file! And KABOOM, you're done, to test, go directly to your config.php and see if it works if you need help with this tutorial feel free to comment! Wildo
  23. Nice to have you around, I'm sure we'll have some fun Wildo
  24. sorry for the bump, but I understand you need a tutorials CMS? I wrote a 3 part tutorial on making a tutorial CMS, here are the links, the final link also has the full script as attachement. http://www.techtuts.com/forums/index.php?s...amp;#entry22650 http://www.techtuts.com/forums/index.php?s...amp;#entry22700 http://www.techtuts.com/forums/index.php?s...amp;#entry23789 Wildo
  25. hmm, what about creating a skin that will match the banner on top, I love the header, but the skin is just the default one isn't it. People will stay at your site earlier when the layout is eye-candy. Wildo

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.