<?php
# session
session_start();
# check that session is set and is valid
if(!isset($_SESSION['login']))
{ header('Location: login.php');
}
?>
<body>
<div class="maincontainer">
<div class="keywordhead">
<div align="center"><img src="Images/keyword_title.png" width="243" height="56" /></div>
</div>
<div class="results">
<p>
<?php
$kword = $_POST["kword"];
function boldText($text, $kword) {
return str_replace($kword, "<strong>$kword</strong>", $text);
}
$testin1 = substr($kword,0,1);
if($testin1 == "") {
print "<strong>No Keyword or a Keyphrase Entered, Please return to the '<a href='keyword_search.php'>Keyword Search Page</a>'</strong>";
}
else {
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
}
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and keywords like '%$kword%';")
or die(mysql_error());
echo "<table border=0 cellpadding=10>";
echo "<tr align = center bgcolor=white>
<td><b>Company Name</b></td><td><b>Section</b></td><td><b>Question</b></td><td><b>Answer</b></td>" ;
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td width = 130px>".boldText($info['company_name'], $kword) . "</td> ";
echo "<td width = 60px>".boldText($info['section_name'], $kword) . " </td>";
echo "<td width = 300px>".boldText($info['question'], $kword) . " </td>";
echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>";
}
echo "</table>";
?>
</p>
</div>
<div class="footer"><a href="logout.php"><br />
Logout</a> | Index | Back</div>
</div>
</body>
</html>
I am relatively new to PHP, and i was curious as to whether a certain function is possible. I have a keyword Search and the code for the results page is above. I would like to bold wherever the $kword variable appears on the page. is this possible?
Thanks
You can create a function to do so, and call it prior to echo'ing the variables.
Instead of: $info['question'] use boldText($info['question'], $kword)
function boldText($text, $keyword) {
return str_ireplace($keyword, "<strong>$keyword</strong>", $text);
}
As a side note, don't forget to escape $kword with mysql_real_escape_string() before using it in a SQL query, or even better, consider using MySQLi or PDO since mysql extension is strongly discouraged
Can you use something like this in each of your Print statements?
str_replace($kword, "<b>$kword</b>", $info[...])
(or CSS e.g.
<span style='font-weight:bold'>...</span>
if you prefer).
Related
I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post
Right guys here is my big ass question, I'm just a begginer and all of that so it's probably just a silly and easy thing but how from this code
<?php
session_start();
if(!isset($_SESSION['password']))
{
header('Location:index.php');
exit();
if ($_SESSION['logged_in']= 0)
{
header('Location:index.php');
exit();
}
}
?>
<?php include 'includes/header.php';?>
<div class="page_menu_box">
<ul>
<li>USER PROFILE</li>
<li>ABOUT US</li>
</ul>
</div>
<div class="page_content">
<table class="viewbook_table">
<tr>
<th>ISBN</th>
<th>AUTHOR</th>
<th>DATE WHEN PUBLISHED</th>
<th>COVER</th>
</tr>
<?php
include("core/database/connect.php");
$connection = #new mysqli('localhost','root','','bs_admin_tools');
$sql = "SELECT * FROM books";
$query = $connection->query( $sql );
while ($row = mysqli_fetch_array($query))
{
echo "<tr>";
echo "<td width = 15%>";
echo $row['ISBN'];
echo "</td>";
echo "<td width = 15%>";
echo $row['author'];
echo "</td>";
echo "<td width = 15%>";
echo $row['datepublished'];
echo "</td>";
//check whether an image is available for the record chosen
if ( $row['imgdir'] )
{
//create a variable which holds data from the images folder
$imageDir = "image/";
//create a second variable which holds the value of the image linked to the record selected
$img = $imageDir . $row['imgdir'];
/*link to the directory*/
echo "<td width ='15%' padding='30%'>";
//display the image
echo "<left><a href='viewdetails'><img src='$img'></a>";
echo "</td></tr>";
}
//echo "<br />"; //display a line break
}//end of the while loop
//release connection from database
mysqli_close($connection);
?>
</table>
</div>
<?php
echo "<div class='page_menu_box'><ul><li>
".$_SESSION['login']."
</ul></li></div>'";
include 'includes/footer.php';?>
I just want to basically click on the $img and view the book details, in other words I make one of the variables into a hyperlink and whenever someone is transferred onto another page I want a variable to be sended to that file where I would facilitate the file to intake the variable and display other shit about that book using 'Select from books where $theVariable="XD"'. I would be really happy if anyone could help me, I'm bouncing my head over a wall for the 2nd day to work that out.
I can only do stuff on php so please solutions only involving php or html ;_;
I'm not sure about the title, I tried my best.
I have a table displayed with information from a database using this file
display.php
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("tournaments") or die(mysql_error());
$result = mysql_query("SELECT * FROM tournies")
or die(mysql_error());
echo '<table id="bets" class="tablesorter" cellspacing="0" summary="Datapass">
<thead>
<tr>
<th>Tournament <br> Name</th>
<th>Pot</th>
<th>Maximum <br> Players</th>
<th>Minimum <br> Players</th>
<th>Host</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>';
while($row = mysql_fetch_array( $result )) {
$i=0; if( $i % 2 == 0 ) {
$class = "";
} else {
$class = "";
}
echo "<tr" . $class . "><td>";
echo $row['tour_name'];
$tour_id = $row['tour_name'];
echo "</td><td>";
echo $row['pot']," Tokens";
echo "</td><td class=\"BR\">";
echo $row['max_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['min_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['host'];
echo "</td><td>";
echo "<input id=\"delete_button\" type=\"button\" value=\"Delete Row\" onClick=\"SomeDeleteRowFunction(this)\">";
echo "</td><td>";
echo "<form action=\"join.php?name=$name\" method=\"POST\" >";
echo "<input id=\"join_button\" type=\"submit\" value=\"Join\">";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Basically I want the user to press a button from a row of the table and they go to a new page called join.php. I need the persons username and the name of the tournament from the row the clicked.
For example here's my page:
When they click the join button at the end of row one it should send them to
'join.php?name=thierusernamehere&tourname=dfgdds'
Any help much appreciated. Thanks.
echo '<td>Join</td>'
There are many way to approach.
The easiest way is just echo 'JOIN';
or you can use a form with hidden input and submit button.
BUT
Your code is really a mess, try to make your code more maintainable and readable. And do NOT use any mysql_* functions, they are deprecated.
Read more about PDO:
http://php.net/manual/en/book.pdo.php
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
Here is my code so far
<form method="post" name="search">
<input type="search" name="k" placeholder="Enter Keywords">
<input type="submit" value="Search">
</form>
<?
$skw = $_POST['k'];
if(isset($_POST['k'])){
$connect = mysql_connect('*', '*', '*')or die('Could not execute command. Please contact a administrator.');
mysql_select_db('*')or die('No database');
$query = "SELECT * FROM *** WHERE items LIKE '%$skw%'";
$result = mysql_query($query)or die(mysql_error());
echo "<table align=\"left\" border=\"1\" cellpadding=\"5\" cellspacing=\"3\">";
echo "\"" . $skw . "\"";
if(empty($result)){echo " Not Found!";}else{
echo " Found In";
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo "<font color=\"#CC0000\">" . $row['tub_id'] . "</font>";
echo "</td><td>";
echo $row['items'];
echo "</td></tr>";
}
echo "</table>";
}}?>
How do I highlight the $skw in the result? I have tried using preg_replace, but i am very new to php and cannot get it to work. I understand that I can use classes to style it.
Thank you for any help.
First add a css high lightlight
.highlight{background-color: yellow; }
then assuming that $skw is the search keyword and you will search it in row['items']
in your while statement
while($row = mysql_fetch_array($result)){
echo "<tr><td>";
echo "<font color=\"#CC0000\">" . $row['tub_id'] . "</font>";
echo "</td><td>";
$items = preg_replace("/" . $skw. "/", "<span class='highlight'>'" . $skw . "</span>",$row['items']);
echo $items;
echo "</td></tr>";
}
then some notes are: don't use the <font> tag. -_-
and mysql_* is deprecated.. :D
You can add a class
css sytle:
.hihglight{background-color: yellow;}
php code:
echo "<span class='hihglight'>{$skw}</span>";
As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
A useful link Why shouldn't I use mysql_* functions in PHP
here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.