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
Related
This code was working perfectlty as most of you said. I had a little issue with the path pointing to this script. The problem now is that I am having issues with the hyperlink with the href code line. I have a field in my database that is labled fulltext . I am trying to create a script that allows me display the content of fulltext (echo "{$row['fulltext']}.";) when I click on the Read More button. The hyperlink should be populated with the echo "{$row['title']}.";
What mistake am I making by inserting a href="fulltext.php?=$row['fulltext']
<table>
<?php
$dbhost = 'localhost';
$dbuser = 'myusernm';
$dbpass = 'mypwd';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT title, introtext, created, created_by, catid FROM mydb_items';
mysql_select_db('muslimtimes360');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<tr>'; echo '<td>'; echo '<span class="post-date">'; echo "{$row['created']}."; echo '</span>'; echo '</td>'; echo '</tr>';
echo '<tr>';
echo '<td>'; echo '<h2 class="blog-post-title">'; echo "{$row['title']}."; echo '</h2>'; echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'; echo '<p>'; echo "{$row['introtext']}."; echo '</p>'; echo '</td>'; echo '</tr>';
echo '<p>'; echo '<tr>';
echo '<td>'; echo ''; echo '<input type="button" value="Read More" />'; echo ''; echo '</td>';
echo '</tr>';
echo '</div>';
echo '<div class="blog-meta">';
echo '<img src="img/avatar.png" alt="Avatar" />';
echo '<tr>'; echo '<td>'; echo '<h4 class="blog-meta-author">'; echo "{$row['created_by']}."; '</h4>';
echo '<span>'; echo 'Category:'; echo "{$row['catid']}."; echo '</span>'; echo '</td>'; echo '</tr>';
echo '</table>';
}
echo "";
mysql_close($conn);
?>
Please note mysql_fetch_array only need data (array), no need to give MYSQL_ASSOC, because you're conflating MySQL and MySQLi
and
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Change this
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
to this:
while($row = mysql_fetch_array($retval))
and inside while just use:
echo $row['created'];
No need of use like this echo "{$row['created']}".
You are Using Old Mysql Syntax Which is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0.
If you are using Latest version of PHP then your code will not Work.Change the appropriate Syntax and Try again.
Try remove the first line table and put inside
example
while($row = mysql_fetch_array($retval))
{
echo '<table>';
I have tried to run your code with only adjusted connection credentials and table/column names and it works fine (using php 5.5 and php 5.3).
Isn't possible there is no record in the database? Another possibility could be some typo.
Edit: Also you have only one table opening tag while as many closing tags as there is records present. Try to inspect source code of the 'blank page'.
Edit2: Try running this snippet instead of your former code and let us know what happens:
<?php
// connection string constants
define('DSN', 'mysql:dbname=muslimtimes360;host=localhost');
define('USER', 'myusernm');
define('PASSWORD', 'mypwd');
// pdo instance creation
$pdo = new PDO(DSN, USER, PASSWORD);
// query preparation
$stmt = $pdo->query("
SELECT title, introtext, created, created_by, catid
FROM mydb_items
");
// fetching results
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// if this returns 0 then it means no records are present
echo count($result) . "\n";
// the loop should print valid table
foreach ($result as $index => $row) {
if ($index == 0) echo '<table>';
echo <<<HTM
<tr>
<td>
<span class="post-date">{$row['created']}</span>
</td>
</tr>
<tr>
<td>
<h2 class="blog-post-title">{$row['title']}</h2>
</td>
</tr>
<tr>
<td>
<p>
{$row['introtext']}
</p>
</td>
</tr>
<tr>
<td>
<p>
<input type="button" value="Read More" />
</p>
</td>
</tr>
<tr>
<td>
<div class="blog-meta">
<img src="img/avatar.png" alt="Avatar" />
<h4 class="blog-meta-author">{$row['created_by']}</h4>
<span>Category: {$row['catid']}</span>
</div>
</td>
</tr>
HTM;
if ($index == (count($result)-1)) echo '</table>';
}
Im trying to make a datalist which will have the options as:
ItemCode - Name
Code and Name are in the same table but are in different columns
Ive got it working for just code:
<?php
while($res=mysql_fetch_row($code))
{
$fullname=$res[0];
echo "<option value=$fullname></option>";
}
?>
I tried doing the following for both:
<?php
while(($res=mysql_fetch_row($code)) && ($res2=mysql_fetch_row($name)))
{
$fullname=$res[0]." - ".$res2[0];
echo "<option value=$fullname></option>";
}
?>
However I had no joy, any help would be greatly appreciated.
echo "<option value=$fullname>$fullname</option>";
//instead of $module?
Try this:
<?php
$query="select ItemCode,Name from tablename";
$result=mysql_query($query);
$cols=2;
echo "<table>";
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++)
{ $row=mysql_fetch_array($result);
?>
<td>
<table>
<tr valign="top">
<td>
<b><?=$row['ItemCode'] ?></b><br />
<?=$row['Name'] ?><br />
</td>
<td width="50"> </td> <!-- Create gap between columns -->
</tr>
</table>
</td>
<?
}
} while($row);
echo "</table>";
?>
You may want to use something like this:
while ($row = mysql_fetch_assoc($result)) {
$value = $row['item_code'] . " - " . $row['name'];
echo '<option value="' . $value . '">' . $value . '</option>';
}
Note: mysql_query, mysql_fetch_row and mysql_fetch_assoc are deprecated, use something else.
This 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.
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. 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.
<?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).