Display only 20 characters from content? Display paragraphs? - php

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

Related

combine three query and get specific error alert

how can i brief this queries?
i want to combine them but get specific error alert and different three variable
<?php
$sql = "SELECT content FROM post where title='tv'";
$sql2 = "SELECT content FROM post where title='radio'";
$sql3 = "SELECT content FROM post where title='net'";
$result = $connection->query($sql);
$result2 = $connection->query($sql2);
$result3 = $connection->query($sql3);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$tvPrice = $row['content'];
}
}else{
echo "tv error";
}
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$radioPrice = $row['content'];
}
}else{
echo "radio error";
}
if ($result3->num_rows > 0) {
while($row = $result3->fetch_assoc()) {
$netPrice = $row['content'];
}
}else{
echo "net error";
}
?>
Using the IN() you can return only those rows with a title of 'tv', 'radio' and 'net'. Then add the title to the query selection so you know which results a re which.
Then just amend you code to fetch all the results into a rows array and then check for the entries and report errors accordingly
<?php
// make a connection to the database obviously :)
$sql = "SELECT title, content
FROM post
WHERE title IN('tv', 'radio', 'net')";
$result = $connection->query($sql);
$rows = $result->fetch_all(MYSQLI_ASSOC);
// better initialise the variables in case you try using them later
$tvPrice = $radioPrice = $netPrice = 0;
foreach ($rows as $row){
if ($row['title'] == 'tv')){
$tvPrice = $row['content'];
}
if ($row['title'] == 'radio') {
$radioPrice = $row['content'];
}
if ($row['title'] == 'net') {
$netPrice = $row['content'];
}
}
if ( $tvPrice == 0 ) echo 'tv error';
if ( $radioPrice == 0 ) echo 'radio error';
if ( $netPrice == 0 ) echo 'net error';
?>
Usually you don't actually need tree (or more) variables; use an array.
<?php
$titles = ['tv', 'radio', 'net'];
// Generate the query
$sql =
"SELECT content, title FROM post WHERE title IN("
. implode(", ", array_map(function( $title ) use( $connection ) {
return '"' . mysqli_real_escape_string($connection, $title) . '"';
}, $titles) )
. ")";
$result = $connection->query($sql);
$prices = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Check for duplicates
if( !empty( $prices[ $row['title'] . 'Price' ] ) )
echo $row['title'] . " has duplicate error";
else
$prices[ $row['title'] . 'Price' ] = $row['content'];
}
}
// Check if we have all desires rows
foreach( $titles as $title )
if( empty( $prices[ $title . 'Price' ] ) )
echo "$title missing error";
// If you really need tree variables instead of array:
extract($prices);

Can't get data from MySQL

I'm connected to phpmyadmin but I can not get any data from phpmyadmin.
my php version is 7.2.9 , I made everything that I wanted in database but php can't show the data in site ( I'm using localhost ).
here is the code:
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
mysql_connect("localhost", "root", "");
mysql_select_db('search');
$query = mysqli_query($query);
$numrows = mysqli_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</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysql_close();
?>
You have a couple of mistakes in your PHP/HTML. I'm gonna sum them up here so you can take a look at them:
<h2><a href='$link'>$title</h2></a>$description<br /><br /> This is wrong HTML. Close your a tag inside the h2.
You are connecting to you database through mysql, but querying through mysqli. Connect to your database with mysqli. Mysql_ family of functions have been removed in PHP 7
You have a typo in your query. you have written FORM instead of FROM.
You are exploding your $_GET variable on spaces. But i doubt if a $_GET variable has any spaces to begin with... Check if this is true.
first of all mysql_connect() is not anymore available after php 5. Instead of using mysql use mysqli_connect(). Please how to make connection and query database using php7 here.
https://www.w3schools.com/php/func_mysqli_fetch_row.asp
If still you have problem. Ask for help in comments.
<?php
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn = mysqli_connect("localhost", "root", "","search");
if(!$conn)die("Connection Error");
$query = mysqli_query($conn,$query);
if(!query)die("query error");
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while ($row = mysqli_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\"";
}
You are mixing up mysqli and mysql. I have edited your stuff. Please try. Don't forget to fix the portion marked [missing]
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= " description OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
if($result= mysqli_query($conn, $query)){
$numrows = mysqli_num_rows($result);
}
if($numrows > 0){
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
//echo "<h2><a href='$link'>$title</h2></a>
//$description<br /><br />";
echo '<h2><a href="' . $link . '">' . $title .
'</h2></a>' . $description . '<br /><br />';
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysqli_close();
?>
Here's the Full working Example TESTED at my end (Last night I wasn't at my work machine and couldn't test the code.Later I created a small db and Tested it. I had searched with the dummy key 'ram mary albert' in my example
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$qu1 = "SELECT * FROM search WHERE ";
$qu2 = "order by id ASC";
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
for($i=0; $i< count($terms); $i++){
$query = $qu1 . " keywords LIKE '%$terms[$i]%' " . $qu2;
echo( $query . "<br>" );
$resulter= mysqli_query($conn, $query);
while($row = mysqli_fetch_array($resulter)){;
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$EscLink='\'' . $link . '\'';
echo ('<a href="javascript:void(0)" onClick="alert(' .
$EscLink . ')">' . $title . '</a><br>' . $description .
'<br /><br />');
} // Close While
} // Close for
//disconnect
mysqli_close($conn);
?>

search feature not work for certain words

The problem I faced is when search word "anti" it will display the product but when search based words "anti wrinkle" mentioned no search result. Please refer below for the code and advice what do I missed.
$search_output = '';
if(isset($_POST['search'])) {
$searchsql = $_POST['search'];
$searchsql = preg_replace('#[^a-z 0-9?!#-]#i', '', $searchsql);
$query = mysql_query("SELECT tb_spa_prd.*, tb_spa_prd_cat.spa_prd_cat FROM tb_spa_prd AS tb_spa_prd INNER JOIN tb_spa_prd_cat ON tb_spa_prd.spa_prd_cat_id = tb_spa_prd_cat.spa_prd_cat_id WHERE spa_prd_cat LIKE '%$searchsql%' or spa_prd_code LIKE '%$searchsql%' or spa_prd_name LIKE '%$searchsql%'") or die ("Could no search!");
$count = mysql_num_rows($query);
if($count == 0) {
$search_output .= '<tr>
<td colspan="9" style="text-align: center">There was no search results!</td>
</tr>';
} else {
$row_no = 1;
while($row = mysql_fetch_array($query,MYSQL_ASSOC)) {
$spa_prd_id = $row["spa_prd_id"];
$spa_prd_cat = $row["spa_prd_cat"];
$spa_prd_code = $row["spa_prd_code"];
$spa_prd_name = $row["spa_prd_name"];
$spa_prd_vlm = $row["spa_prd_vlm"];
$spa_prd_qty = $row["spa_prd_qty"];
$spa_prd_crt_date = $row["spa_prd_crt_date"];
$spa_prd_crt_usr = $row["spa_prd_crt_usr"];
$search_output .= '<tr>
<td>'.$row_no++.'</td>
<td>'.$spa_prd_cat.'</td>
<td>'.$spa_prd_code.'</td>
<td>'.$spa_prd_name.'</td>
<td>'.$spa_prd_vlm.'</td>
<td>'.$spa_prd_qty.'</td>
<td>'.$spa_prd_crt_date.'</td>
<td>'.$spa_prd_crt_usr.'</td>
<td><a href=\'edit_spa_prd.php?spa_prd_id='.$spa_prd_id.'\'>Edit</a> | <a href=\'delete_spa_prd.php?spa_prd_id='.$spa_prd_id.'\'>Delete</a></td>
</tr>';
} // close while loop
}
}
You can try explicitly including a LIKE predicate for each search term like this:
$searchsql = $_POST['search'];
$terms = explode(" ", $searchsql);
$likecols = array("spa_prd_cat", "spa_prd_code, "spa_prd_name");
$likesql = "";
foreach ($likecols as $likecol) {
for ($i = 0; $i < count($terms); ++$i) {
if ($likecol == "spa_prd_cat" && $i == 0) {
$likesql = "spa_prd_cat LIKE $terms[0]";
}
else {
$likesql .= " OR $likecol LIKE $terms[$i]";
}
}
}
$querystr = "SELECT tb_spa_prd.*, tb_spa_prd_cat.spa_prd_cat FROM tb_spa_prd AS tb_spa_prd INNER JOIN tb_spa_prd_cat ON tb_spa_prd.spa_prd_cat_id = tb_spa_prd_cat.spa_prd_cat_id WHERE ";
$querystr .= $likesql;
$query = mysql_query($querystr) or die ("Could no search!");

Why Getting only 1 array instead of many arrays?

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();

Displaying a $result as a result for a Search

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

Categories