che_anj 08-16-2007, 03:07 PM I would like to minimize writing a lot of codes just to have those textbox while I think theres a way to shorten it.. Anyone can help how.. this is my code..
<?php
echo ' <form name="adduser" method="post">';
function testing()
{
for($i = 0; $i < 6; $i++)
{
echo '<tr><td><input type="text" name="a['.$i.']"></td></tr>';
}
}
echo '<table width="50%" cellpadding="2" cellspacing="0" class="table-outline">
<tr><td class="detail-title">Add User</td>
</tr>
</table>
<table width="50%" cellpadding="2" cellspacing="0" class="table-list">';
echo testing();
echo '<tr><td colspan="2" class="table-list-title">
<input type="submit" name="add" value="Register">
<input type="reset" name="add" value="Cancel"></td></tr>
</table></form>';
so far its the looping is work, but I want also the Caption should be present like ..
Username:<input type="text" name="a[0]">
Name:<input type="text" name="a[1]">
....
and what about If im going to insert the values of the textboxes? HOw could I do it since its on array form...
mysql_query("insert into users (username,password...)VALUES('$_POST[]')");
tnx...
rafiki 08-16-2007, 03:13 PM foreach?
che_anj 08-16-2007, 03:26 PM do u know what to apply the foreach,, i try this
$text= array("Username","Password","Name","Email");
foreach($text as $names)
{
echo $names;
}
but how to apply it together with the looping of textboxes.. Im confuse..
CFMaBiSmAd 08-16-2007, 04:09 PM In your foreach example, $text is an array, yes?
From your form, $_POST['a'] will be an array -
[a] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
)Using your foreach code -
foreach($_POST['a'] as $names)
{
echo $names;
}
Bahamut 08-16-2007, 04:47 PM function testing($data_array)
{
foreach ($data_array as $key => $val)
{
echo "<tr>";
echo "<td>$val :</td>";
echo "<td><input type='text' name='$key'></td>";
echo "</tr>";
}
}
$text_array = array('username'=>'Username','password'=>'Password','name'=>'Name','email'=>'Email');
echo "<form name='adduser' method='post'>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
echo "<tr>";
echo "<td class='detail-title'>Add User</td>";
echo "</tr>";
echo "</table>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
testing($text_array);
echo "<tr>";
echo "<td colspan='2' class='table-list-title'>";
echo "<input type='submit' name='add' value='Register'>";
echo "<input type='reset' name='reset' value='Cancel'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
che_anj 08-17-2007, 09:11 AM Great! it works .. Thanks a lot Bahamut...
che_anj 08-17-2007, 10:18 AM By the way, What about if im going to include the type of the input..
example. echo "
<td><input type='$type' name='$key'></td>";
because i have a password type of input..
tnx..
Bahamut 08-17-2007, 10:53 AM function testing($data_array)
{
foreach ($data_array as $val)
{
echo "<tr>";
echo "<td>$val[text] :</td>";
echo "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
echo "</tr>";
}
}
$text_array = array(
'username' => array (
'name'=> 'username',
'text'=> 'Username',
'type'=> 'text',
'size'=> 25,
'max' => 25
),
'password' => array (
'name'=> 'password',
'text'=> 'Password',
'type'=> 'password',
'size'=> 20,
'max' => 20
),
'email' => array (
'name'=> 'email',
'text'=> 'Email',
'type'=> 'text',
'size'=> 30,
'max' => 30
)
);
echo "<form name='adduser' method='post'>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
echo "<tr>";
echo "<td class='detail-title'>Add User</td>";
echo "</tr>";
echo "</table>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
testing($text_array);
echo "<tr>";
echo "<td colspan='2' class='table-list-title'>";
echo "<input type='submit' name='add' value='Register'>";
echo "<input type='reset' name='reset' value='Cancel'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
Inigoesdr 08-17-2007, 08:43 PM Don't echo all of that static HTML, there's no reason to, and it's just more work for you:
<?php
function testing($data_array)
{
foreach ($data_array as $val)
{
?>
<tr>
<td><?=$val['text'];?> :</td>
<td><input type="<?=$val['type']?>" size="<?=$val['size']?>" maxlength="<?=$val['max']?>" name="<?=$val['name']?>" /></td>
</tr>
<?
}
}
$text_array = array(
'username' => array (
'name'=> 'username',
'text'=> 'Username',
'type'=> 'text',
'size'=> 25,
'max' => 25
),
'password' => array (
'name'=> 'password',
'text'=> 'Password',
'type'=> 'password',
'size'=> 20,
'max' => 20
),
'email' => array (
'name'=> 'email',
'text'=> 'Email',
'type'=> 'text',
'size'=> 30,
'max' => 30
)
);
?>
<form name="adduser" method="post">
<table width="50%" cellpadding="2" cellspacing="0" class="table-outline">
<tr>
<td class="detail-title">Add User</td>
</tr>
</table>
<table width="50%" cellpadding="2" cellspacing="0" class="table-list">
<?=testing($text_array);?>
<tr>
<td colspan="2" class="table-list-title">
<input type="submit" name="add" value="Register" />
<input type="reset" name="reset" value="Cancel" />
</td>
</tr>
</table>
</form>
Note: If you're going to redistribute the code or if you have short tags off you should use <?php echo $var; ?> instead of <?=$var;?>.
Also, if you're only running the loop once you don't really need to put it in a function.
che_anj 08-21-2007, 08:22 AM Thanks Bahamut, it works well..
Btw, What about if theres a select box? is it possible to include it in the loop? or make another function?
tnx
Bahamut 08-21-2007, 02:25 PM function testing($data_array)
{
foreach ($data_array as $val)
{
echo "<tr>";
echo "<td>$val[text] :</td>";
if ($val['type'] == 'select') {
echo "<td>";
echo "<select size='$val[size]' name='$val[name]'>";
foreach ($val['options'] as $k => $v)
echo "<option value='$k'>$v</option>";
echo "</select>";
echo "</td>";
} else {
echo "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
}
echo "</tr>";
}
}
$text_array = array(
'username' => array (
'name'=> 'username',
'text'=> 'Username',
'type'=> 'text',
'size'=> 25,
'max' => 25
),
'password' => array (
'name'=> 'password',
'text'=> 'Password',
'type'=> 'password',
'size'=> 20,
'max' => 20
),
'email' => array (
'name'=> 'email',
'text'=> 'Email',
'type'=> 'text',
'size'=> 30,
'max' => 30
),
'gender' => array (
'name'=> 'gender',
'text'=> 'Gender',
'type'=> 'select',
'size'=> 1,
'options' => array ('M'=>'Male','F'=>'Female')
)
);
echo "<form name='adduser' method='post'>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
echo "<tr>";
echo "<td class='detail-title'>Add User</td>";
echo "</tr>";
echo "</table>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
testing($text_array);
echo "<tr>";
echo "<td colspan='2' class='table-list-title'>";
echo "<input type='submit' name='add' value='Register'>";
echo "<input type='reset' name='reset' value='Cancel'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
che_anj 08-21-2007, 03:13 PM Brilliant!!! Tnx Bahamut... Ur great!...
che_anj 08-21-2007, 03:49 PM one last question Bahamut pls forgive me...
Im using function in my select box..
example..
//select box function
function html_select($name,$value,$arrayof,$extra='',$selectedvalue='')
{
$str = "<select name='$name' class='small'>";
$str .="<option value='0'>Choose One</option>";
foreach($arrayof as $key=>$val)
{
if($key==$selectedvalue)
$str .="<option value='$key' selected>$val</option>";
else
$str .="<option value='$key'>$val</option>";
}
$str.="</select>";
return $str;
}
//function for Query Code
function array_of($table)
{
global $link;
switch ($table)
{
case'users':
$query="select * from users";
break;
case 'coun':
$query="select id,value from countries";
break;
case 'comp':
$query="select * from client";
break;
}
$result = dbquery($link, $query);
while($row=mysql_fetch_array($result))
{
$arrayof[$row[0]]=$row[1];
}
return $arrayof;
}
//calling the function for displaying the select box
'.html_select('countries',0,array_of('coun')).'
tnx..
Bahamut 08-21-2007, 04:07 PM foreach ($data_array as $val)
{
echo "<tr>";
echo "<td>$val[text] :</td>";
if ($val['type'] == 'select') {
echo html_select($val['name'], '', $val['options'], '', $val['value']);
} else {
echo "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
}
echo "</tr>";
}
'country' => array (
'name'=> 'country',
'text'=> 'Country',
'type'=> 'select',
'size'=> 1,
'value' => 'USA',
'options' => array_of('coun')
)
che_anj 08-21-2007, 04:24 PM Great! Bahamut ur the best... Thanks a lot...Its a big help...
che_anj 08-22-2007, 09:40 AM really sorry... what about if i have this code below to include in the loop
$day=("31")+1;
<select name="days" class="dtext"><option value="0"></option>';
for($i=$day-31;$i < $day; $i++)
{
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
</select>
$months_array = array("January","February","March","April","May","June","July","August","September","October","November","December");
<select name="month" class="dtext">
foreach($months_array as $months)
{
<option value=\"$months\">$months</option>
}
</select>
tnx...
Bahamut 08-22-2007, 11:26 AM function testing($data_array)
{
foreach ($data_array as $val)
{
echo "<tr>";
echo "<td>$val[text] :</td>";
if ($val['type'] == 'select') {
echo html_select($val['name'], '', $val['options'], '', $val['value']);
} else if ($val['type'] == 'select_date') {
echo "<td>";
html_date($val['name'], $val['value']);
echo "</td>";
} else {
echo "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
}
echo "</tr>";
}
}
function html_date($name, $value)
{
echo "<select name='".$name."_day' class='dtext'>";
echo "<option value='0'></option>";
for($i=1;$i < 32; $i++)
{
$selected = (substr($value,3,2) == $i) ? ' selected ' : '';
echo "<option value='$i' $selected>".$i."</option>";
}
echo "</select>";
$months_array = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo "<select name='month' class='dtext'>";
foreach($months_array as $mon => $months)
{
$selected = (substr($value,0,2) == $mon+1) ? ' selected ' : '';
echo "<option value='".$name."_mos' $selected>$months</option>";
}
echo "</select>";
}
$text_array = array(
'username' => array (
'name'=> 'username',
'text'=> 'Username',
'type'=> 'text',
'size'=> 25,
'max' => 25
),
'password' => array (
'name'=> 'password',
'text'=> 'Password',
'type'=> 'password',
'size'=> 20,
'max' => 20
),
'email' => array (
'name'=> 'email',
'text'=> 'Email',
'type'=> 'text',
'size'=> 30,
'max' => 30
),
'my_date' => array (
'name' => 'my_date',
'value'=> date("m-d"),
'text'=> 'Date',
'type'=> 'select_date',
'size'=> 1,
)
);
echo "<form name='adduser' method='post'>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-outline'>";
echo "<tr>";
echo "<td class='detail-title'>Add User</td>";
echo "</tr>";
echo "</table>";
echo "<table width='50%' cellpadding='2' cellspacing='0' class='table-list'>";
testing($text_array);
echo "<tr>";
echo "<td colspan='2' class='table-list-title'>";
echo "<input type='submit' name='add' value='Register'>";
echo "<input type='reset' name='reset' value='Cancel'>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
Bahamut 08-22-2007, 11:40 AM Correction
echo "<select name='".$name."_mos' class='dtext'>";
foreach($months_array as $mon => $months)
{
$selected = (substr($value,0,2) == $mon+1) ? ' selected ' : '';
echo "<option value='".($mon+1)."' $selected>$months</option>";
}
echo "</select>";
che_anj 08-22-2007, 11:57 AM GREAT!!! Thank you very much Bahamut... I have nothing to say but your really great. Thanks for the time spending on my question... Keep it up... Send my regards to you.. can I get ur email ad.. Just kidding.. hehehe...
che_anj 08-23-2007, 10:45 AM wrong post
che_anj 08-24-2007, 09:21 AM I have a query that will display the result in the same form.
$result =mysql_query("SELECT $table.username,$table.password,$table.email,$table.bdate from $table where candidate_id='$candid'");
$row=mysql_fetch_array($result);
function testing($data_array)
{
foreach ($data_array as $val)
{
echo "<tr>";
echo "<td>$val[text] :</td>";
if ($val['type'] == 'select') {
echo html_select($val['name'], '', $val['options'], '', $val['value']);
} else if ($val['type'] == 'select_date') {
echo "<td>";
html_date($val['name'], $val['value']);
echo "</td>";
} else {
echo "<td><input type='$val[type]' size='$val[size]' maxlength='$val[max]' name='$val[name]'></td>";
}
echo "</tr>";
}
}
function html_date($name, $value)
{
echo "<select name='".$name."_day' class='dtext'>";
echo "<option value='0'></option>";
for($i=1;$i < 32; $i++)
{
$selected = (substr($value,3,2) == $i) ? ' selected ' : '';
echo "<option value='$i' $selected>".$i."</option>";
}
echo "</select>";
$months_array = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo "<select name='month' class='dtext'>";
foreach($months_array as $mon => $months)
{
$selected = (substr($value,0,2) == $mon+1) ? ' selected ' : '';
echo "<option value='".$name."_mos' $selected>$months</option>";
}
echo "</select>";
}
$text_array = array(
'username' => array (
'name'=> 'username',
'text'=> 'Username',
'type'=> 'text',
'size'=> 25,
'max' => 25,
'value'=> "$row[username]"
),
'password' => array (
'name'=> 'password',
'text'=> 'Password',
'type'=> 'password',
'size'=> 20,
'max' => 20,
'value'=> "$row[password]"
),
'email' => array (
'name'=> 'email',
'text'=> 'Email',
'type'=> 'text',
'size'=> 30,
'max' => 30,
'value'=> "$row[email]"
),
'my_date' => array (
'name' => 'my_date',
'value'=> date("m-d"),
'text'=> 'Date',
'type'=> 'select_date',
'size'=> 1,
)
);
/// How could i put the $row[bdate] while theres already value date("m-d") that occupies.
tnx.
Bahamut 08-24-2007, 11:21 AM function html_date($name, $value)
{
echo "<select name='".$name."_day' class='dtext'>";
echo "<option value='0'></option>";
for($i=1;$i < 32; $i++)
{
$selected = (substr($value,8,2) == $i) ? ' selected ' : '';
echo "<option value='$i' $selected>".$i."</option>";
}
echo "</select>";
$months_array = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo "<select name='".$name."_mos' class='dtext'>";
foreach($months_array as $mon => $months)
{
$selected = (substr($value,5,2) == $mon+1) ? ' selected ' : '';
echo "<option value='".($mon+1)."' $selected>$months</option>";
}
echo "</select>";
echo "<select name='".$name."_year' class='dtext'>";
for($yctr=1950;$yctr<date("Y")+1;$yctr++)
{
$selected = (substr($value,0,4) == $yctr) ? ' selected ' : '';
echo "<option value='$yctr' $selected>$yctr</option>";
}
echo "</select>";
}
'my_date' => array (
'name' => 'my_date',
'value'=> $row['bdate'],
'text'=> 'Date',
'type'=> 'select_date',
'size'=> 1,
)
che_anj 08-24-2007, 12:20 PM correct me if im wrong..
What I did to with the day, month and year to save in the database..
$bdate="$_POST[my_date_day]-$_POST[my_date_mos]-$_POST[my_date_yearr]";
insert it as $_POST[bdate]
and so the output is 1-10-1981
I tried to call it but it does not display correctly..
Bahamut 08-24-2007, 12:39 PM $bdate="$_POST[my_date_year]-$_POST[my_date_mos]-$_POST[my_date_day]";
che_anj 08-24-2007, 12:59 PM Thank you, it works well now..
che_anj 08-24-2007, 01:05 PM do you have idea how to put a breakline in a paragraph?
example..
$query="select * from jobs";
..
..
then will display the result $row[description]
the row description is a form of paragraph how to it a breakline?
tnx.
Bahamut 08-28-2007, 09:44 AM echo nl2br($row['description']);
|
|