I have recently completed my search engine but now I have a new challenge.
This following code I am using it to read out values from a callflow table in my DB and displaying them in a table letting u know wether the call was answered yes or no.
if(isset($res))
{
//creating table
echo '<table style="width:1500px; cell-padding:4px; cell-spacing:0; margin:auto;">';
echo'<th>Time</th><th>Answered Y/N</th></th><th>Naam</th><th>Caller ID</th>';
while($result = mysql_fetch_assoc($res))
{
echo '<tr>';
echo '<td>'.$result['statusCalling'].'</td>';
if ($result['statusAnswered'] =="NULL"||$result['statusAnswered'] =="Null" || $result['statusAnswered'] =="null" || $result['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo '<td>'.$result['calleridname'].'</td>'.'<td>'.$result['calleridnum'].'</td>' ;
echo '</tr>';
}
echo '</table>';
}
I need now to display these results in a search engine result!
I tried this but I doesnt work! No idea how else to go about this! Please help!
$output = '';
//collect
if(isset($_POST['asd'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query('SELECT * FROM callflow WHERE statusCalling LIKE "%'.$searchq.'%" OR calleridname LIKE "%'.$searchq.'%" OR calleridnum LIKE "%'.$searchq.'%" OR $results LIKE "%'.$searchq'%"');
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)) {
$statusCalling = $row['statusCalling'];
$calleridname = $row['calleridname'];
$calleridnum = $row['calleridnum'];
$results = $row['statusAnswered'];
$id = $row['ID'];
$output .= '<div>'.$statusCalling.' '.$calleridname.' '.$calleridnum.' '.$results.'</div>';
}
}
}
I know that mysql is deprecated, I am learning to program still and i figure if I don't know mysql I cant learn pdo because I don't understand what is what. Please help!
I've worked out the answer and I am posting it here so others can see a way of solving this when they researching for something similar.
<?php
mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("voizxl_wachtrij") or die("Could not find Database");
$output = '';
//collect
if(isset($_POST['asd'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query('SELECT * FROM callflow WHERE statusCalling LIKE "%'.$searchq.'%" OR calleridname LIKE "%'.$searchq.'%" OR calleridnum LIKE "%'.$searchq.'%"');
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)) {
$statusCalling = $row['statusCalling'];
$calleridname = $row['calleridname'];
$calleridnum = $row['calleridnum'];
$id = $row['ID'];
$output[] = $row;
}
}
}
?>
<?php foreach($output as $o){;
if($o['statusAnswered']){
echo $o['statusCalling'].' Answered: '.$o['calleridname'].' '.$o['statusAnswered'].' '.$o['calleridnum'].'<br />';
}else{
echo $o['statusCalling'].' Not Answered: '.$o['calleridname'].' '.$o['calleridnum'].'<br/>';
}
}?>
<br/><br/><br/>
<?php
Cheers
Related
I have separate question really which I need help. I only want to display say 20 characters from 'content'.
<?php
$output = '';
if(isset($_GET['q']) && $_GET['q'] !== ' ') {
$searchq = $_GET['q'];
$q = mysqli_query($db, "SELECT * FROM article WHERE title LIKE '%$searchq%' OR content LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
if($c == 0) {
$output = 'No search results for <strong>"' . $searchq . '"</strong>';
} else {
while($row = mysqli_fetch_array($q)) {
$id = $row['id'];
$title = $row ['title'];
$content = $row ['content'];
$output .= '<a href="article.php?id=' .$id. '">
<h3>'.$title.'</h3></a>'.$content.'';
}
}
} else {
header("location: ./");
}
print("$output");
mysqli_close($db);
?>
i will answer your first question:
insert this line after:
$content = $row ['content'];
if(strlen($content)>20) $content=substr ($content,0,19);
I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();
I'm currently using
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords = '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("localhost", "root", "password");
mysql_select_db("search");
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
}
else
echo "No Search Results have been found for \"<b>$k</b>\"";
// disconnect
mysql_close();
?>
this piece of code I learned from a tutorial to begin my search engine. I'll be honest, I don't know any PHP, and understanding what's going on here has been a real struggle.
How do I manipulate this piece of code such that if I search "Swarthmore College" or "Swarthmore", Swarthmore results will show up. I need to make sure that just typing "College" or "swar" or "a" won't trigger the Swarthmore results.
But I need common words like "University", "of", and "Chicago" to trigger together for college searches like "University of Chicago." It should also be triggered by abbreviations like "UChicago."
I'm sorry if this is a lot to ask, but I am completely lost.
Thank you in advance.
You should include an elseif clausule. The code would be:
if ($numrows == 1) {
// You dont need to iterate if you want to show only unique result
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
elseif ($numrows > 1)
echo "More than one Search Results have been found for \"<b>$k</b>\". Please be more specific ";
else
echo "No Search Results have been found for \"<b>$k</b>\"";
I am trying to run a simple PHP search script that will show look through my mySQL database and present the results on a separate page that I've created. When I run the code, my results don't show up. Can someone please help me?? This is the code that i'm currently using:
<?php
error_reporting(E_A`enter code here`LL);
ini_set('display_errors', 1);
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
$i = 0;
foreach ($terms as $each){
$i++;
if ($i == 1)
{
$query .= "keywords LIKE '%$each%' ";
}
else
{
$query .= "OR keywords LIKE '%$each%' ";
}
}
// connect
mysql_connect("josetogbecom.fatcowmysql.com","username","password");
mysql_select_db("gff_hff6a144eg");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}
}
else
{
echo "No Search Results Were Found for \"<b>$k<b>\"";
}
// disconnect
mysql_close();
?>
Thanks in advance!
Check if your $i counter var is set to $i=0 before foreach loop
Bug at this line:
echo "No Search Results Were Found for \"<b>$k<b>/"";
Should be:
echo "No Search Results Were Found for \"<b>$k<b>\"";
Bug at this line:
mysql close();
Should be:
mysql_close();
Bug at this line:
$i++;
You should always initialize your variables, like this:
$i = 0;
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '$each%' ";
else
$query .= "OR keywords LIKE '$each%' ";
}
If the script still doesn't work after correcting these two bugs, add to the beginning of the script these 2 lines and tell us what you see:
error_reporting(E_ALL);
ini_set('display_errors', 1);
I have a custom search function on my site, using PHP. I don't like having to go to my database every time I make a page, so can I write something to automatically add a new page to my database? Here's my code:
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("*****", "*****", "*****");
mysql_select_db("*****");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3>
<hr />";
}
}
else
echo "No Results Found for \"<b>$k</b>\". Try Again.";
//disconnect
mysql_close();
?>