...
  • Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp
KEEP IN TOUCH

Most Popular

  • osTickets > Reports v4.2 (18056)
  • HTPC > Setup Windows 7 as a Media Center with XBMC (10962)
  • osTicket > View headers for original email message (4173)
  • WebDev > Allow PHP in Wordpress Widgets (3615)
  • Web Dev > Password protect webpage(s) with PHP & LDAP (2475)

Posts by Scott Rowley

Web Dev > Handling dynamically created checkboxes

May02
2012
Leave a Comment Written by Scott Rowley

Ok, so let’s say I have a list of users in a database. I want to get that list out on the screen and allow someone to be able to use checkboxes to do something with each of those users. In this example we’re going to say we are deleting any users that have been checked. If you are not already familiar with building dynamic output from mysql then I suggest you first read over Web Dev > Populate PHP/HTML table from MySQL database

First we’ll build our form to show our users, in this example I’m actually building my list of users from a query to a mail server which gets returned to me first as a string. I need to change this into an array so I’m going to use explode(). I’ll go through this all step by step so it’s easy to follow along and you can skip whatever parts you feel you are already familiar with.

So lets say the mail server returns this string to me of users:

$members_string = 'Bruce Chuck Jackie Jet';
echo "Members string is '$members_string'";

Which gives us an output of

Members string is 'Bruce Chuck Jackie Jet'

Since I want to loop through each of these members I need to first create an array that I can loop through, this is where explode() comes in.

// Members are separated by spaces, so explode on spaces
$members = explode(' ',$members_string);

READ MORE »

ajax loader
Posted in PHP, Web Development - Tagged checkbox, dev, dynamic, form, php, web, web dev, Web Development
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

Web Dev > PHP function, check if a MySQL table exists

May01
2012
Leave a Comment Written by Scott Rowley

Note: This is NOT original sudobash.net code. This has been copied over from ElectricToolBox.com, full credit goes to him/her (I couldn’t find a name anywhere).

The PHP function below gets passed in a tablename and an optional database name. If the database name is not passed in then it retrieves it using the MySQL function SELECT DATABASE(). It then queries the MySQL information schema to see if the table exists and then returns either true or false.

function table_exists($tablename, $database = false) {

    if(!$database) {
        $res = mysql_query("SELECT DATABASE()");
        $database = mysql_result($res, 0);
    }

    $res = mysql_query("
        SELECT COUNT(*) AS count
        FROM information_schema.tables
        WHERE table_schema = '$database'
        AND table_name = '$tablename'
    ");

    return mysql_result($res, 0) == 1;

}

The PHP MySQL functions are used in the above example. A database connection is assumed and there is no error checking, but you can modify it to utilise whatever database library / abstraction layer you are using in your project and improve how you see fit.

To use the function you’d do something like this:

if(table_exists('my_table_name')) {
    // do something
}
else {
    // do something else
}

and if you wanted to specify the database name as well (perhaps you are needing to query if the table exists in multiple databases other than the one you are currently connected to), you’d do this:

if(table_exists('my_table_name', 'my_database_name')) {
    // do something
}
else {
    // do something else
}
ajax loader
Posted in MySQL, PHP, Web Development - Tagged check, database, dev, exists, function, MySQL, php, table, web, Web Development
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

WebDev > Fixing Warning: “Headers already sent” & “Cannot modify header information”

Mar24
2012
Leave a Comment Written by Scott Rowley

Eventually in your web development you may run into one of these annoying errors. What it typically means is that your browser is being sent some HTML before the PHP command header() and it’s causing your issue.

Unfortunately there is no easy way to go about troubleshooting this other than following the logic through your code, hopefully you’ve saved & tested recently and have a fairly short code to go troubleshooting with. In video games I learned early on to “Save early, save often”. I’ve since modified that to my programming so that I remember to save early, save often but also “Test early, test often”. Make little changes, then you know how much you need to go back and troubleshoot when there is an error. Anyway, another less known cause of this issue is that you’ve got the dos character ^M inserted in your code. This recently happened to me when I used FTP to download a website and then uploaded it somewhere else. Somewhere along the line my windows PC decided my perfectly fine files should have some ^M inserted into some of the files. As a result I got the “Headers already sent” error.

I’d run into the error before but I knew that I hadn’t introduced any new code so I knew it couldn’t be any html being sent before the header() function. After much googling and “facedesk”‘ing I finally found that the dos character could also cause this error. After a bit more googling I found that the easiest way to get rid of this is to simply run the following command and it will easily and quickly remove all of the dos characters (^M).

dos2unix your_messed_up_file.php

I ran this command on the file and then tried my test again and just like that I was back to normal again :)

ajax loader
Posted in Web Development - Tagged already, cannot, development, dos2unix, fix, fixed, fixing, headers, information, modify, php, save, sent, test, troubleshoot, warning, web
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

WebDev > Alternate table row color with PHP & CSS

Mar19
2012
Leave a Comment Written by Scott Rowley
<style type='text/css'>
 tr.odd {
       background: #d0d0d0;
 }
</style>
 echo <?php '<tr'.(($c = !$c)?' class="odd"':' class="even"').'>'; ?>
ajax loader
Posted in PHP, Web Development - Tagged css, echo, php, row, script, table
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

WebDev > Allow PHP in WordPress Widgets

Jan26
2012
Leave a Comment Written by Scott Rowley

The following will allow for you to post PHP in your widgets:

Place in your functions.php file
Appearance > Editor > Theme Functions (functions.php)

add_filter('widget_text', 'php_text', 99);

function php_text($text) {
 if (strpos($text, '<' . '?') !== false) {
 ob_start();
 eval('?' . '>' . $text);
 $text = ob_get_contents();
 ob_end_clean();
 }
 return $text;
}

Now you can place php code so long as its within its <?php and ?> tags.
Credit

ajax loader
Posted in Web Development - Tagged allow, functions, php, theme, widget, wordpress
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

WebDev > CSS Stylings

Jan25
2012
Leave a Comment Written by Scott Rowley

Just starting to make a note of some of my CSS creations:

Informational Box

SudoBash is proud to announce our newest office location

123 Kittens Street conveniently located 1 mile from LOLcat lane.

 <style type='text/css'>
.info {
  border: 2px solid #D8D8D8;
  margin: 10px 0px;
  background-repeat: no-repeat;
  padding: 10px 0 10px 150px;
  background-position: 10px center;
  color: #00529B;
  background-color: #E0E0E0;
  background-image: url('/images/info.png');
}
</style>

<div class="info"><strong>SudoBash is proud to announce our newest office location</strong><br><br>

123 Kittens Street conveniently located 1 mile from LOLcat lane.</div>
ajax loader
Posted in Web Development - Tagged css, dev, development, html, info, information, web, Web Development
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

BASH > Recursively set file/directory permissions

Jan19
2012
1 Comment Written by Scott Rowley

Move to the directory you want to start chmodding files in and run the following:

Recursively set FILE permissions.

find . -type f -exec chmod 644 {} \;

Recursively set DIRECTORY permissions.

find . -type d -exec chmod 755 {} \;

*Note, obviously you can change the chmod number to whatever you want, 777, 600, etc.

ajax loader
Posted in BASH - Tagged chmod, chmodding, directory, file, files, find, permission, recursive, recursively, set, type
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

Web Dev > Select first n words of a sentence

Jan16
2012
Leave a Comment Written by Scott Rowley

Useful for giving a teaser of a post and then including … on the end followed by “Read More”.

$sentence = "This is my sentence, there are many like it but this one is mine. What do you think of it?";
$n = 10; // Customize to however many words you want to actually show
$teaser = implode(' ', array_slice(explode(' ', $sentence), 0, $n));

echo $teaser."...READ MORE

Which should result in:
This is my sentence, there are many like it but…READ MORE

ajax loader
Posted in PHP, Web Development - Tagged array_slice, customize, echo, explode, implode, n, php, post, read more, sentence, teaser
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

Web Dev > Keep your copyright date current automatically

Jan12
2012
Leave a Comment Written by Scott Rowley

Here’s a quick little tip I’ve been using recently with the new year having rolled around. I’ve started replacing all of my © 2011 code with the following. This will always return the date of the current year. That way you don’t have to remember to update it all over the place every time we “celebrate” a new year.

<?php echo date('Y'); ?>

So now I don’t ever have to update this post and the current year should always be reflected here: 2012

ajax loader
Posted in PHP, Web Development - Tagged automagically, automatic, automatically, copy, copyright, current, date, echo, php, year
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail

Web Dev > Populate PHP/HTML table from MySQL database

Jan11
2012
2 Comments Written by Scott Rowley

Hey again all, for this post I’ll be covering how to populate a PHP/HTML table by way of looping through a table in mysql. I’ll be using the sample database provided by http://www.mysqltutorial.org/mysql-sample-database.aspx which has to do with models (cars, planes, ships, etc). Everyone has differing levels of knowledge so I’ll be including some basics as well such as connecting to the mysql database (and closing it later on).

The table we’ll be using in the database is ‘products’. It has the following columns:

productCode        - A unique inventory number
productName        - Name of the product
productLine        - Basic descriptor, 'Motorcycles', 'Classic Cars', etc
productScale       - This models scale size
productVendor      - Company that built the model
productDescription - Detailed description of product
quantityInStock    - Current number of quantity in stock
buyPrice           - Listed price on the "website"
MSRP               - Manufacturers Suggested Retail Price

If I know I’m going to be using my mysql database in multiple files I’ll always throw the connection in something like a ‘dbconnect.php’ file. Here’s an example:

<?php
  mysql_connect("localhost", "MYSQL_USERNAME", "MYSQL_PASSWORD") or die(mysql_error());
  mysql_select_db("MYSQL_DATABASE") or die(mysql_error());
?>

Now you can include this in every file, or better yet in your header file which will get included everywhere else. So for example in your header.php file you could throw in:

<?php
  require_once('dbconnect.php');
?>

Alright, so now you’ve got your connection to your database and the appropriate database selected. We’ll skip over the other content that you want to eventually add and say (for this example) that we want to list all of our models. We’ll look at doing this a few different ways, first off we’ll go simple and just request everything from the database and then we’ll tell php how to spit that all out to us.
READ MORE »

ajax loader
Posted in MySQL, PHP, Web Development - Tagged connect, html, limit, loop, MySQL, nest, nested, php, query, result, select, sql, where, while
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« Older Entries

Be Heard!

Authors needed! Feel like sharing your tech wisdom with the world? We are looking to expand our writer base and would love to hear from you. We need articles on any relevant technology/software/media/howto/etc (Well...lets at least hold to the legal stuff ;)

Just email scott (at) sudobash (dot) net

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Sudo Bash Member sites

Des Moines, Iowa Karate Classes
Iowa MMA Tournaments
Iowa SAR
Ooo-er
5point Studios Tattoo & Piercing

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

RSS HowToGeek

  • Week in Geek: Aero Glass Feature will be Removed from Windows 8
  • How to Speed Up Web Browsing with Search & Bookmark Keywords
  • Geek Trivia: Pagers Were First Developed To Assist Which Professionals?
  • The Best Tips and Tricks for Using Email Efficiently
  • Desktop Fun: Starry Skies Wallpaper Collection Series 2

RSS TheGeekStuff

  • UNIX / Linux Processes: C fork() Function
  • How to Calculate IP Header Checksum (With an Example)
  • How to Encrypt Your Bash Shell Script on Linux Using SHC
  • Intro to DOCSIS Architecture, CM CMTS Protocol for Cable Modems
  • Ettercap Tutorial: DNS Spoofing & ARP Poisoning Examples

RSS LifeHacker

  • DIY Maple-Flavored Pancake Syrup [Breakfast]
  • This Week's Top Downloads [Download Roundup]
  • Make Mini Dutch Baby Pancakes in a Muffin Tin [Breakfast]
  • Pour Vinegar Down Your Drain Every Three Months to Keep Clogs Away [Video]
  • Use the Jelly Pocket Method for a Better Drip-Free PB&J [Food Hacks]

EvoLve theme by Blogatize  •  Powered by WordPress Sudo Bash
By Geeks - For Geeks

Back to Top