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

Posts tagged dev

Web Dev> Handling dynamically created checkboxes

May02
2012
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 »

Posted in PHP, Web Development - Tagged checkbox, dynamic, form, php, web, web dev, Web Development

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

May01
2012
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
}
Posted in MySQL, PHP, Web Development - Tagged check, database, exists, function, MySQL, php, table, web, Web Development

WebDev> CSS Stylings

Jan25
2012
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>
.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>
Posted in Web Development - Tagged css, development, html, info, information, web, Web Development

Web Dev> Use onFocus to clear a value and onBlur to replace empty values

Oct17
2011
Written by Scott Rowley

The following can be used to clear a value. I regularly use this for website search bars.

<script>
function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }
</script>
<input type="text" value="email goes here" onfocus="blank(this)" onblur="unblank(this)" />

This post was updated using code found here, which I think works a little nicer.

Posted in Web Development - Tagged clear, development, html, Javascript, onclick, this, value, web

PHP> Export simple MySQL query to .csv file

Aug08
2011
Written by Scott Rowley

The following will allow you to export your mysql queries from mysql to a csv file that can be opened in several spreadsheet softwares. You may need to change the , (comma) to a ; (semi-colon) depending on your software.

A note for those of you using my osTicket Reports MOD: This is not what I’m using for that.

<?php
$host = 'localhost';
$user = 'userName';
$pass = 'password';
$db = 'databaseName';
$table = 'tableName';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Posted in MySQL, PHP, Web Development - Tagged csv, development, excel, export, file, libre, MySQL, office, open, php, web

Web Dev> PHP created random password

Jul22
2011
Written by Scott Rowley
<?php
// The letter l (lowercase L) and the number 1
// have been removed, as they can be mistaken
// for each other.
function RandomPass() {
// Comment the first line to exclude special characters
$chars = "!@#$%^&*";
$chars .= "abcdefghijkmnopqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
srand((double)microtime()*1000000);
$i = 1;
$pass = '' ;
// Number of digits we'd like to have our password be
$n = 8;
while ($i < $n) {
$num = rand() % strlen($chars);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
// Usage
$password = RandomPass();
echo "Random password is: $password";
?>
Posted in Web Development - Tagged create, development, microtime, password, php, rand, random, return, srand, substr, web, while

Web Dev> Meta-Redirect

Apr27
2011
Written by Scott Rowley

I keep forgetting this and I keep needing it — so I’m just finally going to make note of it here:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/index.html">

The zero in this case is the amount of time before a reload (so if you wanted to you could say something like “This page is no longer here — redirecting you in 3 seconds” and then change it to a 3. This code needs to be placed within the <head> tags.

Posted in Web Development - Tagged development, meta, redirect, web

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

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top