Im busy creating a webpage that issues drawings to a client and creates a receipt for sending to a client. The part that I'm having trouble with is to select the drawings that I want to add to the receipt and to change the revision.
The information for the drawings are split over two tables. The first contains the drawing number, title, drawn by etc and the second contains only the revision history. The tables are linked by the drawing id.
To issue a drawings the user must tick the checkboxes and change the revision of the drawings he wants to issue.
I want to input this into the database, but I'm not sure how to create the array. Currently my controller takes gets all the dropdown inputs and only the ticked checkboxes. I want only the dropdown inputs associated with the selected checkboxes to be updated by the database.
This is my view.
<h1>Issue drawing</h1>
<br>
<div id="body">
<div class="row">
<div class="form-group-sm"><lable class="col-sm-2 control-label">Project number:</lable>
<?php
$js = 'onchange="this.form.submit()" class="form-control" id="focusInput"';
echo form_open('dwg_issue/issue_dwg');
echo "<div class=\"col-xs-2\">" . form_dropdown('project_no',$proj_num, $this->input->post('project_no'), $js)."</div>";
echo form_error('project_no', '<div class="col-xs-4"><div class="alert alert-danger fade in">×','</div></div>');
?>
</div>
</div>
<?php
echo "<noscript>".form_submit('submit','Submit')."</noscript>";
echo form_close();
?>
<form action="<?php echo base_url() . 'index.php/dwg_issue/issue'; ?>" method="post" accept-charset="utf-8" id="issue">
<table title="Client information" class="table table-hover">
<caption><b>List of drawings</b></caption>
<thead>
<tr><th>Project number</th><th>Drawing number</th><th>Client drawing number</th>
<th>Title</th><th>Drawn by</th><th>Revision</th><th>Drawn Date</th><th>Select</th>
</thead>
<tbody>
<?php
// var_dump($client_info);
if(!empty($result))
{
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row->project_no . "</td>";
echo "<td>" . $row->sws_dwg_no . "</td>";
echo "<td>" . $row->client_dwg_no . "</td>";
echo "<td>" . $row->dwg_title . "</td>";
echo "<td>" . $row->dwg_by . "</td>";
$rev = array('0' => $row->dwg_rev);
$rev_change = array(
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E'
);
$dropdown = array_merge($rev,$rev_change);
echo "<td>" . form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown) . "</td>";
echo "<td>" . date('Y/m/d', strtotime($row->dwg_date)) . "</td>";
echo "<td>" . form_checkbox('select['.$row->dwg_id.']',$row->dwg_id) . "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
<div class="row">
<div class="col-xs-12 col-md-10">Please select client distribution here with other options</div>
<?php
echo "<div class=\"col-xs-6 col-md-2\"><button class=\"btn btn-md btn-primary btn-block\" type=\"submit\" form=\"issue\">Issue drawings</button></div>";
?>
</div>
<br>
<?php
echo form_close();
?>
</div>
My controller
public function issue_dwg()
{
$data['proj_num'] = $this->model_proj->proj_num_all();
if ($this->input->post('project_no') != '0')
{
$data['result'] = $this->model_issue->list_dwg($this->input->post('project_no'));
}
$data['main_content'] = 'issue_view';
$this->load->view('includes/template.php', $data);
}
//This function takes the selected drawings and create and issue slip
public function issue()
{
$rows = array();
$num_results = count($this->input->post('select'));
for($i = 0; $i < $num_results; $i++)
{
$rows[$i]['select'] = $_POST['select'][$i];
$rows[$i]['dwg_rev'] = $_POST['dwg_rev'][$i];
}
}
The issue_dwg() function gets populates the dropdown with the drawing numbers and populates the view with the db results.
The issue() function needs to process the posted information and convert it into a array to pass to the db.
(I wanted to add an image of the view page, but I don't have enough reputation to do it)
When you parse your form, you need to build your inputs so that, when you submit the form, you end up with associative arrays:
currently you have the following:
//...
echo form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown);
echo form_checkbox('select['.$row->dwg_id.']',$row->dwg_id);
//...
when submitted, it will send the following post array:
"select" (array)[
0 => "some select value",
1 => "some select value"
],
"dwg_rev" (array)[
0 => "some dwg_rev value",
1 => "some dwg_rev value"
]
SOLUTION:
structure your form like this:
//...
echo form_dropdown('result['.$row->dwg_id.'][dwg_rev]',$dropdown);
echo form_checkbox('result['.$row->dwg_id.'][select]',$row->dwg_id);
//...
this way, youll end up with an array as such:
"result"[
0 => [
"select" => "some select value",
"dwg_rev" => "some dwg_rev value"
]
1 => [
"select" => "some select value",
"dwg_rev" => "some dwg_rev value"
]
]
Now you can iterate with a foreach in your controller with ease.
Related
Kinda new to PHP, trying to display only the file name for uploaded pdf's in my custom info box, not the complete url or ID. Been at it for two days searching the net and trying the code examples on acf homepage.
this is my current code:
<div class="post-content">
<?php the_content(); ?>
<!-- advanced custom fields plugin content -->
<?php
if( function_exists('get_field')) {
if (get_field('product_info_content') OR get_field('product_info_file') OR get_field('product_info_file_2') ) {
echo '<div class="info-box">';
echo '<h1>' . get_field('product_info_title') . '</h1>';
the_field('product_info_content');
echo '<ul>';
echo '<li class="product_info_list_item">';
the_field('product_info_file_1');
echo '</li>';
echo '<li class="product_info_list_item">';
the_field('product_info_file_2');
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?><!-- end advanced custom field -->
what I'm trying to active with little to no luck is echoing out the name of the file of product_info_file_1 and 2 as an link that opens in a new window.
Any help would be appreciated!
I believe, your field name is not uniform in if check and in display. You can try following code. This will display the link of file uploaded.
<?php
if( function_exists('get_field')) {
if (get_field('product_info_content') OR get_field('product_info_file_1')
OR get_field('product_info_file_2') ) {
echo '<div class="info-box">';
echo '<h1>' . get_field('product_info_title') . '</h1>';
the_field('product_info_content');
echo '<ul>';
echo '<li class="product_info_list_item">';
$file_1 = get_field('product_info_file_1');
if ( ! empty( $file_1 ) ) {
echo '' . esc_html( $file_1['title'] ) . '';
}
echo '</li>';
echo '<li class="product_info_list_item">';
$file_2 = get_field('product_info_file_2');
if ( ! empty( $file_2 ) ) {
echo '' . esc_html( $file_2['title'] ) . '';
}
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?><!-- end advanced custom field -->
Same problem but I used preg_match() to get the last word in the path so that way I can get the file name.
<?PHP
preg_match("/[^\/]+$/",get_field('filename'), $matches);
echo str_replace('-',' ',$matches[0]);
?>
I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.
Is there a way that i can escape >, I have a combo box which has an option below. I tried replacing > with > but it's still not uploading properly.
<select name="Category3" id="Category3">
<option value="Request |Running > 1 hour">Request |Running > 1 hour</option>
What it should upload is just Request |Running > 1 hour
but what is being uploaded is: Request |Running > 1 hour 1 hour' />
i dont know where its getting the other 1 hour' />
i tried removing the > and it just uploaded Request |Running 1 hour without the excess 1 hour' /> but i need it for consistency
here is the php code that im using to upload to db
<?php
error_reporting(0);
require 'include/DB_Open.php';
$Category3 = mysql_real_escape_string ($_POST['Category3']);
if (isset($ABC))
{
$sql="INSERT into XXX category_2
VALUES ('".$Category3."')";
$result=mysql_query($sql)or die(mysql_error());
}
?>
here is my code for retrieving the data
$sql="SELECT category_2
FROM XXX
WHERE resolved_date BETWEEN '" . $date . "' AND '" . $date1 . "'
ORDER BY resolved_date";
$myData = mysql_query($sql);
//to count if there are any results
$numrow = mysql_num_rows($myData);
if($numrow == 0)
{
echo "No results found.";
}
else
{
echo "$numrow";
}
{
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><strong>Category 3</strong></th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='retrieve.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
Whenever you're displaying text that might include special HTML characters, you should use htmlentities() to encode it properly, so these characters don't cause the HTML to be misparsed (or worse, allow script injection).
$cat2 = htmlentities($info['category_2']);
echo "<td align='center'>" . $cat2 . "<input type='hidden' name='category_2[]' value='" . $cat2 . "' /> </td>";
You also were missing the ' after value=. I've added that (and quotes around all the other attributes).
Since you're creating multiple inputs with the same name, you need to give them an array-style name so that the server script can get all the values. $_POST['category_2'] will be an array.
I have a form with only one input. When I click the button, an AJAX request is made and the data is sent to the response.php, which uses the pdo library to insert the data to db. Here it is okay, but in the page, there is a success function which loads the data into the form - when the ajax request completes, the result is blank...I need to refresh(F5) the page to see the data ..
Here is this code:
<?php
include_once("config.php");
if (!empty($_POST)) {
try{
$statement = $conn->prepare("INSERT INTO DIAGNOSTICO (id_paciente, id_doctor, hconsulta) VALUES (?, ?, ?)");
if ($statement->execute(array($_POST['id_paciente'], $_POST['id_doctor'], $_POST['hconsulta'])));
$dbSuccess = true;
} catch (Exception $e) {
$return['databaseException'] = $e->getMessage();
}
{
echo $conn->lastInsertId();
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
}
$dbh = null;
}
?>
It is working, but when in the page, the result returns from the ajax request and shows the id but not the data...I need the data result...can you help me?
best regards!
After using the lastinsertid you have to select data by using
$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
Read http://php.net/manual/en/pdostatement.fetch.php
View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>