I am currently trying to build a form using mysql/php, below is part of the code I have so far
BLOCK#1:
$proceso = mysqli_fetch_assoc($result); // my query returns only one row
...
<form action='actualizar.php' method='Post'>
<?php
foreach(array_keys($proceso) as $key){
echo "<label for='$key'>$key: </label>";
echo "<input name='$key' value='".$proceso[$key]."'><br/>";
}
echo "<input type='hidden' name='View' value='$view'>";
?>
<input type="submit" value="Actualizar">
</form>
This so far is getting me a form where I'm using the field names to generate labels and input boxes where i show the field value. I would like to further format some of the fields using the jquery datepicker, but only for those fields which have a type = Date in the mysql table.
I've been trying mysqli_fetch_field_direct
using something like:
BLOCK#2:
$fields = mysqli_num_fields($result);
for ($i=0; $i < $fields; $i++) {
$field_types[] = $result->fetch_field_direct($i)->type;
}
but in this case I can't get the value, just the type
Is there a straightforward way to get the type and value of a field?
Edited to (try) to simplify:
Let's say I have a field called email
which has type = varchar
and my SQL query generates one result [email protected]
From BLOCK#1 I get:
-------------------------------
Field-Name | Field-Value
email | [email protected]
From BLOCK#2 I get:
-------------------------------
Field-Name | Field-Type
email | varchar
what I would like is to get
-------------------------------
Field-Name | Field-Type | Field-Value
email | varchar | [email protected]
This is because I would like to use the field type to add a css class to the input box (such as to use the datepicker).
Edit: I put the output in a table because I can't sleep...
Okay... see if this is what you want...
This is a table I made for a different SO question:
mysql> describe user;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| User_ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Email | varchar(100) | YES | | NULL | |
| Name | varchar(100) | YES | | NULL | |
| Password | varchar(100) | YES | | NULL | |
| FB_ID | int(11) | YES | | NULL | |
| Total_Score | int(11) | YES | | 0 | |
| add_date | datetime | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
and from the DB:
mysql> select * from user limit 1;
+---------+-------+------+----------+-------+-------------+---------------------+
| User_ID | Email | Name | Password | FB_ID | Total_Score | add_date |
+---------+-------+------+----------+-------+-------------+---------------------+
| 1 | NULL | kim | NULL | NULL | 10 | 2013-11-03 23:04:08 |
+---------+-------+------+----------+-------+-------------+---------------------+
+
1 row in set (0.00 sec)
And the code:
<?php
$mysqli = mysqli_connect("localhost", "root", "", "test");
// this came from http://php.net/manual/en/mysqli-result.fetch-field-direct.php
$mysql_data_type_hash = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
//252 is currently mapped to all text and blob types (MySQL 5.0.51a)
253=>'varchar',
254=>'char',
246=>'decimal'
);
// run the query...
$result = $mysqli->query("select * from user limit 1");
// get one row of data from the query results
$proceso = mysqli_fetch_assoc($result);
print "<table>
<tr>
<th>\$key</th>
<th>\$value</th>
<th>\$datatype</th>
<th>\$dt_str</th>
</tr> ";
// to count columns for fetch_field_direct()
$count = 0;
// foreach column in that row...
foreach ($proceso as $key => $value)
{
$datatype = $result->fetch_field_direct($count)->type;
$dt_str = $mysql_data_type_hash[$datatype];
$value = (empty($value)) ? 'null' : $value;
print "<tr>
<td>$key</td>
<td>$value</td>
<td class='right'>$datatype</td>
<td>$dt_str</td>
</tr> ";
$count++;
}
print "</table>";
mysqli_close($mysqli);
?>
<style>
/* this is css that you don't need but i was bored so i made it pretty...! */
table { font-family:Courier New;
border-color:#E5E8E3; border-style:solid; border-weight:1px; border-collapse:collapse;}
td,th { padding-left:5px; padding-right:5px; margin-right:20px;
border-color:#E5E8E3; border-style:solid; border-weight:1px; }
.right { text-align:right }
</style>
So... to clarify...
You can use these variables in that foreach
to output or use the information however you want: (I am using my first row of output, for the user_id, as an example)
$key
is the column/field name (such as user_id
)
$field_types[$key]
comes from $result->fetch_field_direct($i)->type
(such as 3
)
$mysql_data_type_hash[$datatype]
is the string version of the datatype using the $mysql_data_type_hash
array at the top of the code. This isn't necessary but I included it so this example is more clear. (such as int
)
$proceso[$key] = $value =
is your value for this iteration of the foreach statement (such as 1
)
Output:
$key $value $datatype $dt_str
User_ID 1 3 int
Email null 253 varchar
Name kim 253 varchar
Password null 253 varchar
FB_ID null 3 int
Total_Score 10 3 int
add_date 2013-11-03 23:04:08 12 datetime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With