Php checkbox retrieve data from database - php

I have:
$array_worker['$worker_id']=$worker_name;
$array_job['$job_id']=$job_name;
I have no problem with dynamic create table with checkbox and store data in database:
The data are stored in table as worker_id,job_id!
Normally, worker may work more than one job, so I create multidimensional array from table in which the stored data!
$array_worded['$worker_id'][]=$job_id;
My question is:
How create dynamic table with checked checkboxes based on array_worked array?

$table='';
foreach($array_worker as $key=>$value){
$table.=''.$value.''; // worker name
$worker_id = // get worker id from $array_worker
foreach($array_job as $key_job=>$val_job)
{
$job_id = // get job id from $array_job
$checked = false;
foreach( $array_worked[$worker_id] as $key_worked => $val_worked )
{
if( $job_id == $val_worked ) // $val_worked contains $job_id
{
$checked = true;
break;
}
}
$table.='<input type="checkbox"' . ( $checked ? ' checked="checked"' : '') . '/>'.$val_job.''; // all jobs from database
}
$table.='';
}
$table.='';
I may make some mistakes in syntax, but code demonstrates basic principle.

It is so simple :
<input type="checkbox" name="formWheelchair"
<?php
$DATABASE-VALUE = $array_worded['$worker_id'][] = $job_id; // OR WHAT EVER
switch ($DATABASE-VALUE) {
case 0:
echo checked />"
break;
........
}
?>

Related

How to number items in for-loop php

I have this code that select content from database and loop all, and few items are specify to be different by adding method (type-A and type-B) in database, so now i want to give all type-B numbers like 1,2,3 depending on number of type in database.
I could have do this using the id in database but it not number accordingly so i need just assign new number to list how many of it i have.
<?php
$getReply = $reply_stmt->getAll(); // this from my database
if(!is_null($getReply)){
foreach($getReply as $row){
$name = $row->itemname;
$method = $row->method;
}
if($method == 'type-B'){
$number = 1;
$number++;
echo $number."B-class<br/>".$name."".$method;
}else{
echo $name."".$method;
}
}
?>
Now i want to return something like this
1 B-class
Peter type-B
John type-A
2 B-class
Mike type-B
I am not sure but try to something like this...
$getReply = $reply_stmt->getAll(); // this from my database
if(!is_null($getReply)){
$number = 1;
foreach($getReply as $row){
$name = $row->itemname;
$method = $row->method;
if($method == 'type-B'){
echo $number." B-class<br/>".$name." ".$method;
$number++;
}else{
echo $name." ".$method;
}
}
}

php check empty or null in many variables

Hello I have a simple script
This is my script, and try this script
<?php
$user_id = $_REQUEST['user_id'];
$pid = $_REQUEST['pid'];
$nopr = $_REQUEST['nopr'];
$tglpr = $_REQUEST['tglpr'];
$uraianpr = $_REQUEST['uraianpr'];
$jenispr = $_REQUEST['jenispr'];
$nilaipr = $_REQUEST['nilaipr'];
$costproject = $_REQUEST['costproject'];
$remarkpr = $_REQUEST['remarkpr'];
$tmpattachid = $_REQUEST['tmpattachid'];
if ($user_id==NULL)
{
$error "User Id Not Complete";
}
else if ($pid==NULL)
{
$error= "PID Not Complete";
}
echo $error;
?>
I want if $user_id or other variables is empty/null
i confused when other variable have empty/null data
i must typing code many if statement :(
example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null
this show error like this
PID Not Complete
NoPR Not Complete
Tgl Pr Not Complete
Jenis Pr Not Complete
Cost Project Not Complete
Help Me, Thank's.
I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.
You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php
And Set it out like this:
foreach($_REQUEST as $key=>$row){
if (is_null($row) || empty($row)){
$errorText .= "You have no data in your ".$key."<br>";
}
}
unset($key,$row);
Then you can output the $errorText value telling people which rows should be fixed. Easy.
Loop through the $_REQUEST and find the empty or null key.
CODE :
foreach ($_REQUEST as $key => $value) {
if(trim($value) ==='' || is_null($value))
{
echo $key . " is not complete" . "<br/>";
}
}

Saprfc table read with filter

I'm new in php saprfc. I use an our integrated function, but now I need to read lines from a table. But how can I do to read only a few lines, filter the results by a criteria, because this table has some millions of rows.
Is it possible?
Here is a code part, that is use recently, works well and quick on very big tables too. I hope it helps others too.
//Try to connect to SAP using our Login array
$rfc = saprfc_open ($saplogin);
IF (! $rfc ) { ECHO "The RFC connection has failed with the following error:".saprfc_error();EXIT; }
//We must know if the function really exists
$fce = saprfc_function_discover($rfc, "RFC_READ_TABLE");
IF (! $fce ) { ECHO "The function module has failed.";ECHO $rfc;EXIT; }
//Convert to uppercase the name of the table to show
$Table = "HERE_THE_TABLE_NAME";
//Pass import parameters
saprfc_import ($fce,"QUERY_TABLE",$Table);saprfc_import ($fce,"DELIMITER","/");
//Pass table parameters
saprfc_table_init ($fce,"OPTIONS");
saprfc_table_append ($fce,"OPTIONS", array ("TEXT"=>"TABLE_FIELD_NAME = '{$input}'")); //input field, filter by this variable
saprfc_table_init ($fce,"FIELDS");
saprfc_table_append ($fce,"FIELDS", array ("FIELDNAME"=>"INT_UI")); //wanted answer field
saprfc_table_init ($fce,"DATA");
//Call and execute the function
$rc = saprfc_call_and_receive ($fce);
if ($rc != SAPRFC_OK)
{
if ($rfc == SAPRFC_EXCEPTION ) { echo ("Exception raised: ".saprfc_exception($fce)); }
else { echo ("Call error: ".saprfc_error($fce)); }
exit;
}
//Fetch the data from the internal tables
$data_row = saprfc_table_rows ($fce,"DATA");$field_row = saprfc_table_rows ($fce,"FIELDS");
for($i=1; $i<=$data_row ; $i++)
{ $DATA[$i] = saprfc_table_read ($fce,"DATA",$i);$TEST[] = SPLIT("/",$DATA[$i]['WA']); } // get the rows to $TEST variable
//release the function and close the connection
saprfc_function_free($fce);

How to add comments to post with Mysql and PHP

I managed to add comments to the table of Comments but it doesn't appear to the related post. I understand because the foreign key (artID) in the articles table is empty. How do I fetch the primary key from the articles table? Here is the structure of my database.
SQL injections issues will be dealt later. Prepared statements will be done. I just would like to get some help on the query and php function. Thank you.
articles
artID
artTitre
artAuteur
artContenu
artDate
commentaires
commentID
commentPseudo
commentText
artID
commentaires.sql.php
<?php
// INSERT
function insertCommentaire($c){
$PseudoCommentaire = $TexteCommentaire ='';
$PseudoCommentaire = $_POST['PseudoCommentaire'];
$TexteCommentaire = $_POST['TexteCommentaire'];
$IdArticle = $_POST['IdArticle'];
$qryInsertComm = 'INSERT INTO commentaires (commentPseudo,commentText, artID)
VALUES ( \''.$PseudoCommentaire.'\',
\''.$TexteCommentaire.'\',
\''.$IdArticle.'\')
';
if (!mysqli_query($c,$qryInsertComm))
{
die('Error: ' . mysqli_error($c));
}
echo "1 record added";
}
// UPDATE
function updateCommentaire( $IdCommentaire ){
}
// DELETE
function deleteCommentaire( $IdCommentaire ){
}
// CONTROLER //
switch( $action ){
case 'insert' :
$process = insertCommentaire($conn);
if( $process == 'ok' )
header( 'location:index.php?page=home' );
else
$page = 'home';
break;
case 'update' :
$process = updateCommentaire( $_GET[ 'item' ] );
if( $process == 'ok' )
header( 'location:index.php?page=home' );
else
$page = 'home';
break;
case 'delete' :
$process = deleteCommentaire( $_GET[ 'item' ] );
if( $process == 'ok' )
header( 'location:index.php?page=home' );
break;
}
?>
You are using a hidden input val called IdArticle in
<input type="hidden" name="IdArticle" value="" />
To keep track of which article the comment should go against, as shown above in the php script:
$IdArticle = $_POST['IdArticle'];
However, looking at your HTML, you should see that you are passing an empty value to your php script.
To fix this, update your hidden input field like this:
<input type="hidden" name="IdArticle" value="<?php echo $rows['artID'] ?>" />
Now it should work.
How do I fetch the primary key from the articles table?
The same as selecting any other column
SELECT `artID` FROM `articles` WHERE `artID` = ?
That's the only question I can see in your post.

PHP Get Multi Select List Values

I'm trying to write a simple function to construct field names for a form. It works fine if at least one value is selected in a multi-select list but if nothing is selected I get an Undefined index error. Here is what I have:
function mcFieldName($mcFieldName){
$mcField = $_POST[$mcFieldName];
if( !is_array($mcField) ){
if( !empty($mcField) ){
return $mcField;
}else{
return 'n/a';
}
}
if( is_array($mcField) ){
$mcFieldArray = implode(',', $mcField);
return $mcFieldArray;
}
}
$MultiSelect = mcFieldName('mcMultiSelect');
// test
echo $MultiSelect . '<br/>';
Thank you!
You just need to protect yourself from reading a key that does not exist in $_POST:
$mcField = isset($_POST[$mcFieldName]) ? $_POST[$mcFieldName] : null;
Before you try to access an array item make sure it exists with using isset():
if (isset($_POST[$mcFieldName])) {
$mcField = $_POST[$mcFieldName];
...
}

Categories