Below is my code for downloading some files from a webpage.
Everything works fine except target="_blank". This button does nothing even though the link is correct. If I right click and press open in a new tab it works but when i press the button it does nothing.
<?php
while ($row = $result->fetch_assoc()) {
$i+=1;
$name = $row["filename"];
$location = "../uploadedfiles/" . $name;
?>
<div class="row">
<p><hr>Name : <?php echo $row["name"] ?> <br> Contact Details : <?php echo $row["number"] ?><br></p>
</div>
<div class="row">
<p>Date Sent: <?php echo date("F d Y-- H:i:s.", filectime($location)) ?><br></p>
</div>
<div class="row">
<p><p> <?php echo $i ?> ) <?php echo substr($name, 10) ?> <a href="../uploadedfiles/<?php echo $name; ?>" target='_blank'><button type='button' class='btn btn-info'>View / Download </button></a></p></p>
</div>
<?php }
?>
<hr>
<?php
} else {
echo "0 results";
}
$con->close();
The way you have created button within the anchor tag is not one of the best practices to be followed.
Ideally you should do something like this:
View/Download
Hope this helps.
Related
I have some working PHP code and I have recently added a button that allows the user to download the image form the root directory, in the database we put the file name e.g. example.png / example.jpeg and when the user clicks download it opens the image in a new tab
what we need is: if the [proof] is= NULL , the download button disables, otherwise it will be enabled and they can click the button
<?php
// output data of each row
while($row=mysqli_fetch_assoc($designresult)) {
?>
<div class="card mb-4 box-shadow"><div class="card-header">
<h4 class="my-0 font-weight-normal">Job Reference: <?php echo $row["jobRef"]; ?></h4>
</div>
<div class="card-body">
<p><b>Company Name:</b><br> <?php echo $row["companyName"]; ?> </p>
<p><b>Requested:</b><br> <?php echo $row["dateReq"]; ?> </p>
<p><b>Request By:</b><br> <?php echo $row["yourName"]; ?> </p>
<p><b>Graphic Type:</b><br> <?php echo $row["graphicType"]; ?> </p>
<p><b>Double Sided:</b><br> <?php echo $row["doubleS"]; ?> </p>
<p><b>Design Info:</b><br> <?php echo $row["info"]; ?> </p>
<p><b>Purpose:</b><br> <?php echo $row["purpose"]; ?> </p>
<p><b>Proof:</b><br> <?php echo $row["approved"]; ?> </p>
<p><b>Proof Date:</b><br> <?php echo $row["appDate"]; ?> </p>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">Download</a>
</div>
</div>
<?php
}
?>
To my knowledge the link tag does not have a "disabled" attribute. But you can "disable" the link by removing the href attribute.
Something like this: It checks if $row['proof'] has some value (by negating the empty), then it prints out the href if result is true.
<a class="btn btn-success" target="_blank" <?php if(!empty($row['proof'])): ?> href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php endif; ?> >Download</a>
Or maybe better: Check if variable is empty and give the user a hint that it's not available. I think this is the better solution, because then your users will know what's going on.
<?php if(empty($row['proof'])): ?>
<span>No proof available</span>
<?php else: ?>
<a class="btn btn-success" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
<?php endif; ?>
<a
class="btn btn-success"
target="_blank"
<?php if(empty($row['proof'])) echo "disabled"; ?>
href="<?php if(!empty($row['proof'])) echo IMAGE_DIR . $row['proof']; ?>">
Download
</a>
try above code. and add disabled class using this condition
<?php
$state = (empty($row['proof'])) ? "disabled='disabled'" : "";
$class = (empty($row['proof'])) ? "disabled" : "";
?>
<a class="btn btn-success <?php echo $class; ?>" target="_blank" href="<?php echo IMAGE_DIR . $row['proof']; ?>" <?php echo $state; ?>>Download</a>
To disabled the button, you need to use disabled HTML attribute. The code above checks $row['proof'] == NULL. If this statement is true it prints disabled = "disabled" in the button element and it isn't true, it prints nothing.
Assuming that you are using bootstrap, .disabled will grayed out the button.
I have the following codes in my contoller which retrives all the data from the table...Its working fine when I use find instead of findAll... using findAll its showing error 'array to string conversion'.Is there any other way to write the query..
// Posting the full contoller Code//
public function actionSearch()
{
$model = new SearchEmployee();
/*Getting Data From Search Form For Processing */
if (isset($_POST['SearchEmployee'])) {
$category = $_POST['SearchEmployee']['category_id'];
$skills = $_POST['SearchEmployee']['skills'];
$experience = $_POST['SearchEmployee']['experience'];
$model = SearchEmployee::model()->findAll("category_id=:category AND
key_skills like :skill AND experience=:experience", array(
'category'=>$category,
'skill'=>'%'.$skills.'%',
'experience'=>$experience
));
//var_dump($model);
$this->render('search', array('model' => $model));
}
}
//view:search.php:This is my view section it is being used for searching and displaying the results //
Search Employees
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'Category'); ?>
<?php echo $form->dropDownList($model,'category_id',$list,array('empty' =>'(Select a Category')); ?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Skills'); ?>
<?php echo $form->textField($model,'skills'); ?>
<?php echo $form->error($model,'skills'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<div class="view">
<h1>Results </h1>
<div class="view" id="id">
<h4>Name : <?php echo $model ->name; ?></h4>
<h4>Skills: <?php echo $model ->key_skills; ?></h4>
<h4>Category: <?php echo $model ->category; ?></h4>
<h4>Experience: <?php echo $model ->experience; ?> Years</h4>
<h5> <?php echo CHtml::submitButton('VIew Details'); ?> </h5>
</div>
</div>
Is there any other way to write the query...Its still working as have used var_dumb to see the result.
It's not great to put logic like this inside your view but it may help to debug..
replace your results section with this loop in your view and see if it's where your problem lies.
<div class="view" id="id">
<?php
if(count($model)) {
foreach($model as $record) {
echo "<h4>Name : " . $record->name . "</h4>";
echo "<h4>Skills : " . $record->key_skills . "</h4>";
echo "<h4>Category : " . $record->category . "</h4>";
echo "<h4>Experience : " . $record->experience . "</h4>";
echo "<h5>" . CHtml::submitButton('VIew Details') . "</h5>";
}
}
?>
</div>
You need to learn about CGridView to display tabular information from db (or other arrays).
Based on your question, I think you should use gii tool to generate some crud files on your models. That would give you many code samples to learn from. You can activate gii by uncommenting 'gii' module in your protected/config/main.php file.
Then browse to root-of-site/index.php?r=gii
You can find all about gii here Automatic Code Generation | The Definitive Guide to Yii
I have a problem with my search.php file to render me results...
I dont get any strings of error, but when I type an existing keyword I get no results...
The format of the results are the same as viewed in my main content on the website (grid view)...
The code:
<body>
<?php include_once("analyticstracking.php") ?>
<div class='container'> <!--Start of the container-->
<div><?php include("includes/header.php"); ?></div>
<div><?php include("includes/navbar.php"); ?></div>
<div><?php include("includes/left_col.php"); ?></div>
<div class='main_col'>
<div class='main_content'>
<?php
include("includes/connect.php");
if(isset($_GET['search'])){
$search_id = $_GET['q'];
$search_query = "SELECT * FROM games WHERE game_keywords LIKE '%$search_id%'";
$run_query = mysql_query($search_query);
echo '<table>';
$games = 0;
while($search_row = mysql_fetch_array($run_query)){
// make a new row after 9 games
if($games%9 == 0) {
if($games > 0) {
// and close the previous row only if it's not the first
echo '</tr>';
}
echo '<tr>';
}
// make a new column after 3 games
if($games%3 == 0) {
if($games > 0) {
// and only close it if it's not the first game
echo '</td>';
}
echo '<td>';
}
$game_id = $search_row['game_id'];
$game_name = $search_row['game_name'];
$game_category = $search_row['game_name'];
$game_keywords = $search_row['game_name'];
$game_image = $search_row['game_image'];
?>
<div class="game_grid">
<a href="game_page.php?id=<?php echo $game_id; ?>"><img src="images/games_images/<?php echo $game_image; ?>" width="120" height="120" />
<span><?php echo $game_name; ?></span>
</div>
<?php
$games++;
}
}
?>
</table>
</div>
</div>
<div><?php include("includes/footer.php"); ?></div>
</div> <!--End of the container-->
</body>
Any idea?
EDIT:
I solved my problem, its a small mistake I made,
In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidently... now everything works perfectly :)
You have a typo in code
change code as below
if(isset($_GET['search'])){
$search_id = $_GET['search']; //$_GET['q'];
.
.
.
}
I solved my problem, its a small mistake I made, In the HTML form of the search I forgot to give the submit button: "name="search", I removed it accidentally... now everything works perfectly :)
I looked over some solutions, but I coudn't figure out how to get this solved
here is my php
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow($row['contents'])>View details »</a></p>
</div>";
}
I just want to pass the contents into (a function)
and then this function display on click
on div specific id
looking on the web for several hours couldn't help
some say
add
{}
other /' '/
or
'''$row[contents]'''
stil no help
function parashow(x){
//document.getElementById('allrows').style.display = "none";
}
I don't know how to pass $row['contents']
to function argument
and then
get x
and replace it into another div by id ('something')
Try this
while($row = mysql_fetch_array($result))
{
echo"<div class='span4'>
<h2>$row[title]</h2>
<p>$row[intro]</p>
<p><a class='btn' onclick=parashow('".$row['contents']."')>View details »</a></p>
</div>";
}
It's good to generate HTML without PHP:
<?php while($row = mysql_fetch_array($result)) { ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p><a class='btn' onclick="parashow('<?php echo $row['contents']; ?>')">View details »</a></p>
</div>
<?php } ?>
try this
<?php while($row = mysql_fetch_array($result)) : ?>
<div class='span4'>
<h2><?php echo $row[title]; ?></h2>
<p><?php echo $row[intro]; ?></p>
<p>View details »</p>
</div>
<?php endwhile; ?>
I've searched through similar questions but to no avail, so hopefully someone can help.
I'm having an issue paginating the search results on a site I've built using the Zend Framework (1.12). I can retrieve the items and also the pagination is formed but when I click to go to the next page of pagination, all the search results are lost. The code appends ?page=2 e.t.c to the URL but the code then loses the searchitems.
I read in one question about possibly using sessions or Zend_Session in particular but I'm a little lost at present! Relatively new to PHP.
Firstly here is my search form - search-form.php (XHTML)
<?php
// Search Form
?>
<div id="search">
<h4 style="margin-bottom:20px;">Search</h4>
<form name="prodSearch" action="listing-search.php" method="post">
<input type="text" name="searchitems" id="searchitems" />
<input type="submit" name="search_items" id="search_items" value="Go" />
</form>
</div>
This is included in my other pages.
listing-search.php is then as follows
<?php
require_once('admin/scripts/library.php');
require_once('admin/scripts/db_definitions.php');
try {
?>
<!-- HTML Doc Type, Head removed e.t.c -->
<div id="listing-col-main">
<div id="all-cars">
<h4>Search Results</h4>
</div>
<!-- Listings -->
<?php
$query = $_POST['searchitems'];
$min_length = 3;
if (strlen($query) >= $min_length) {
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw = searchItems($dbread, $query);
$paginator = Zend_Paginator::factory($raw);
if (isset($_GET['page'])) {
$paginator->setCurrentPageNumber($_GET['page']);
}
$paginator->setItemCountPerPage(8);
$paginator->setPageRange(3);
$count = $paginator->getTotalItemCount();
if ($count > 0) {
foreach ($paginator as $results) { // repeat all results ?>
<?php
$item_id = $results['item_id'];
$photos = getRelatedPhotos2($dbread, $item_id);
?>
<div class="product-listing">
<div class="product-listing-image">
<?php foreach ($photos as $photo) { //repeat ?>
<img src="image_upload/<?php echo $photo['filename']; ?>" width="75" alt="" />
<?php } ?>
</div>
<div class="product-listing-title">
<h4><?php echo $results['type_name']; ?> - <?php echo $results['item_make'] . ' ' . $results['item_model']; ?> </h4>
</div>
<div class="product-listing-details">
<p>YEAR: <?php echo $results['item_year']; ?> FUEL: <?php echo $results['item_fuel']; ?> TRANS: <?php echo $results['item_transmission']; ?> MILEAGE: <?php echo $results['item_mileage']; ?> </p>
</div>
<div class="product-listing-viewmore">More Info</div>
</div>
<?php } ?>
<!-- 8 products per page -->
<div id="product-listing-pagination">
<?php
$pages = $paginator->getPages('Elastic');
if (isset($pages->previous)) {
echo 'Prev';
}
foreach ($pages->pagesInRange as $page) {
if ($page != $pages->current) {
echo " <a href='" . $_SERVER['PHP_SELF'] . "?page={$page}'>$page</a>";
} else {
echo ' ' . $page;
}
}
if (isset($pages->next)) {
echo ' Next';
}
?>
</div>
<?php } else { ?>
<div id="noresults"> <?php echo "No results found for " . $query; ?> </div>
<?php }
} ?>
</div>
<div id="col-right">
<?php include("search-usedcars.php"); ?>
<!-- End of Main Search -->
<div id="latest-news">
<h3>Latest News</h3>
<?php include("latestnews.php"); ?>
</div>
</div>
<div class="clear"></div>
<div id="footer-bar"> Designed by </div>
</div>
</body>
</html>
<?php
} catch (Exception $e) {
echo $e->getMessage();
}
?>
It is because your query is passed in $_POST, when you click your nav links away you loose the data stored in $_POST and only keep what is in $_GET. Either change your search form method to $_GET and add &searchitems="'.$query.'" to both your nav links, or store the query in $_SESSION, or you can change your nav links to buttons wrapped in forms with method post and include hidden fields for searchitems and page. I would advise changing the search form method to get as it allows the bookmarking of searches and avoids complexity.