Displaying only the file name Advanced Custom Fields - advanced-custom-fields

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]);
?>

Related

Convert two inputs to a single array to input into db codeigniter

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.

current post custom taxonomy link in WordPress

I am trying to get current post custom taxonomy link and name separately to put some schema data and my code is:
<?php $args = array('taxonomy' => 'developer'); ?>
<?php $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<meta itemprop="url" content="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>"></meta>
<a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<span itemprop="name"><?php echo $tax_menu_item->name; ?></span></a>
<?php endforeach; ?>
</div>`
The problem is that the above code is displaying all the "developer" which have at least one post and not the current post taxonomy.
How can I fix this!
<meta itemprop="url" content="<?php $terms = wp_get_post_terms( $post->ID, 'developer'); foreach($terms as $term) {
echo "" . get_term_link($term) . ""; } ?>"></meta>
<?php $terms = wp_get_post_terms( $post->ID, 'developer'); foreach($terms as $term) {
echo '<span itemprop="name">' . $term->name . '</span>'; }
?>

PHP mysql Query Bug

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.

HTML dropdown box with MySQL in form

I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work

How to smash two HTML elements together with multiple tags

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>";
?>