Showing posts with label Learn PHP. Show all posts
Showing posts with label Learn PHP. Show all posts

Friday, July 22, 2011

Make Drop Down Menu From PHP Function

We use be writing a Drop Down with HTML...But I have found simple way to make it short...

Here the older one (HTML):-

<select name="P_State" class="textinput" id="P_State" onChange="display_district11(this.value)" tabindex=5> <option value="">--Please Select--</option>
<?php
do {
?>
<option value="<?php echo $row_state['CD_State']?>"<?php if (!(strcmp($row_state['CD_State'], $row_ins['CD_State']))) {echo "selected=\"selected\"";} ?>><?php echo $row_state['Desc_State']?></option>
<?php
} while ($row_state = mysql_fetch_assoc($state));
$rows = mysql_num_rows($state);
if($rows > 0) {
mysql_data_seek($state, 0);
$row_state = mysql_fetch_assoc($state);
}
?>
</select>


Here is the PHP code that I use...You just need to call this function only when you need to use the down drop..short isn't it?

<?php

$PP_State = '';
$js = "display_dis(this.value,'PP_District','panel_addr','8')";
 State_db('PP_State', 'PP_State', '7', $PP_State, $js);?>

The Code below, you need to put to some place that you can call it...
example : - Create a new page, and other page that you need the Drop Down just include this page...easy...

<?php
function State_db($name, $id, $tab, $exist, $java)
{
$sql = 'SELECT CD_State, Desc_State FROM LT_State ORDER BY Desc_State ASC';
$result = mysql_query($sql);
echo '<select name="'.$name.'" id="'.$id.'" class="textinput" tabindex="'.$tab.'" onchange="'.$java.'">';
echo '<option value="">Please Select</option>';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$cd = $row['CD_State'];
$desc = $row['Desc_State'];
$selected = ($cd==$exist)?'selected':'';
echo '<option value="'.$cd.'" '.$selected.'>'.$desc.'</option>';
}
echo '</select>';

}
?>

Monday, May 23, 2011

Calculating differences between two date in months and days

<?php
$date1 = $TPC_From;
$date2 = $TPC_To;
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d bulan, %d hari\n", $months, $days);  ?>

You can customize the code using your own purposes..

Friday, April 15, 2011

for each in php

1. For each bring one value at a time

<?php

 $fruit
= array( "Banana","Apple", "Plum");

foreach (
$fruit as $fruit_name)
{
    echo 
"$fruit_name<br/>\n";
}
?>
 


The above will display:
Banana
Apple
Plum 
2. For each bring two value in the array


<?php

$fruitColours 
= array("Banana" => "Yellow","Apple"  => "Green","Plum"   => "Purple");

foreach (
$fruitColours as $fruit_name => $fruit_colour)
{
    echo 
"$fruit_name is $fruit_colour<br/>\n";
}
?>




The above will display:
Banana is Yellow
Apple is Green
Plum is Purple

Monday, November 29, 2010

Let Learn PHP (Basic) - Lesson 5

Here's the code to Delete Multiple table at once...



<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'my_db';
$editFormAction = $_SERVER['PHP_SELF'];
if ((isset($_POST["Delete"])) && ($_POST["Delete"] == "Hapus")) {
mysql_select_db($dbname, $conn);
mysql_query("DELETE FROM biodata_ahli WHERE nokp_baru='".$_POST['nokp_baru']."'", $conn)or die(mysql_error());
mysql_query("DELETE FROM pengguna WHERE nokp_baru='".$_POST['nokp_baru']."'", $conn)or die(mysql_error());
}
mysql_select_db($dbname,$conn);
$query_pengguna = "SELECT pengguna.nokp_baru, pengguna.id_pengguna, biodata_ahli.nama FROM pengguna LEFT JOIN biodata_ahli ON biodata_ahli.nokp_baru = pengguna.nokp_baru WHERE pengguna.nokp_baru = '121212121212'";
$pengguna = mysql_query($query_pengguna, $conn) or die(mysql_error());
$row_pengguna = mysql_fetch_assoc($pengguna);
$totalRows_pengguna = mysql_num_rows($pengguna);
?>
<form name="form1" method="post" action="<?php echo $editFormAction; ?>">
<table border="1" bordercolor="#000000" align="center">
<tr><td>
<table width="365" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="131">Nama</td>
<td width="10">:</td>
<td width="224"><?php echo $row_pengguna['nama']; ?></td>
</tr>
<tr>
<td>No. Kad Pengenalan</td>
<td>:</td>
<td><?php echo $row_pengguna['nokp_baru']; ?>
<input name="nokp_baru" type="hidden" value="<?php echo $row_pengguna['nokp_baru']; ?>"></td>
</tr>
<tr>
<td>ID Pengguna</td>
<td>:</td>
<td><?php echo $row_pengguna['id_pengguna']; ?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Delete" id="Delete" value="Hapus"></td>
</tr>
</table>
</td></tr></table>
</form>





Pretty easy is it?

Wednesday, November 24, 2010

Let Learn PHP (Basic) - Lesson 4

Today, i want to show you how to do a drop down menu using database connection.



<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'my_db';
mysql_select_db($dbname);
$query = "SELECT CD_State, Desc_State FROM State";
$result = mysql_query($query);
?>
<select name="state">
<?php do { ?>
<option value="<?php echo $row['CD_State'];?>"><?php echo $row['Desc_State']; ?></option>
<?php } while($row = mysql_fetch_array($result, MYSQL_ASSOC))?>
</select>

OUTPUT 


 


In Programming, we must know to combine all kind of sources in order to get the exact figure that we want. In this post, i show you the drop down menu that i combine the code from Lesson 2 and 3...then we got Lesson 4...Isn't it wonderful...! Programming should be more interesting when we know how to do it...Good Luck!

Tuesday, November 23, 2010

Let Learn PHP (Basic) - Lesson 3

After you got the connection to the database, it time to select the table in the database to your page..let see how it does...



$query = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name : {$row['name']} <br>" .
"Subject : {$row['subject']} <br>" .
"Message : {$row['message']} <br><br>";
}

isn't it simple...:-) 

Let Learn PHP (Basic) - Lesson 2

Working with PHP always need a connection to the database..
Coz that make it easier to build an interactive Web Application.

Here some of the example that you can use:-

Example 1

<?php
$hostname_conn = "localhost";
$database_conn = "my_db";
$username_conn = "root";
$password_conn = "abc123";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>

Example 2

<?php
$con = mysql_connect("localhost","root","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
  
Example 3

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'my_db';
mysql_select_db($dbname);
?>

Let Learn PHP (Basic) - Lesson 1

How to write text in php?

In HTML, we just write whatever we want isn't it? Like this..

Hello World

In PHP, it will be this way..

<?php echo 'Hello World'; ?>

LinkWithin

Related Posts Plugin for WordPress, Blogger...