I have two scripts below. The first script only searches one Column for a user-typed keyword, then it will display the results as a list and make BOLD the characters the user typed. That script works great.
The second script is something I modified to search multiple columns. It searches just fine. The problem is that I cannot get the BOLD (or make STRONG) if the searched value came from the other columns. How do I determine if the searched value came for Column 1, Column 2, Column 3, etc...? If the searched value came from column "DESCRIP" then I want to make the letter bold in the listed value.
First Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE `MUNTERS_PN` LIKE (:keyword) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Second Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE (`MUNTERS_PN` LIKE (:keyword) OR `DESCRIP` LIKE (:keyword) OR `MANUF` LIKE (:keyword) OR `MANUF_PN` LIKE (:keyword) ) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
/******* INSERT CODE TO DETERMINE WHICH COLUMN WAS QUERIED ******/
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
$manuf = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF']);
$manuf_pn = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF_PN']);
$descrip = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['DESCRIP']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Be careful, in the echo in the second script, you use $rs['MANUF'], $rs['MANUF_PN'] and $rs['DESCRIP'] instead of $manuf, $manuf_pn and $descrip.
That is why the replacements don't appear in the output.
So you should use :
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $manuf_pn . '] <br/>' . $descrip . '</span></li>';
Related
Would like to use GROUP_CONCAT in order to retrieve database info (images) from a userid with multiple image entries. These images are in the form of a file path. Would then like to have the images output in a single row per userid.
After extensive research on the matter, I find simple examples of what is needed, but I get lost with the query I currently have in place.
Here is my code as of now:
<table>
<th></th>
<th>Name</th>
<th>Location</th>
<th>High School</th>
<th class="thLargest">Colleges</th>
</table>
<table>
<?php
session_start();
$conn = mysqli_connect("", "", "", "");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT college.id, college.title, GROUP_CONCAT(college.image) AS cImage, users.profileImage, users.fname, users.lname, users.level AS ulevel, users.city, users.state, users.highschool, users.primarysport, usercollege.id, usercollege.collegeid
FROM college, users, usercollege
WHERE usercollege.collegeid = college.id AND usercollege.userid = users.id
ORDER BY usercollege.createdate ASC";
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row['status'] == null) {
echo "";
}
if ($row['ulevel'] == "A Profile") {
echo '<tr>' .
'<td class="small">' . '<a href="http://www.example.com/aprofile.php?user="'.$row["username"] . '>' . '<img src="'.$row['profileImage'].'" />' . '</a>' . '</td>' .
'<td class="large">' . $row['fname']. ", " . $row['lname'] . '</td>' .
'<td class="large">' . $row['city'] . ", " . $row['state'] . '</td>' .
'<td class="large">' . $row['highschool'] . " (" . $row['primarysport'] . ") " . '</td>' .
'<td class="largest">' .
'<div class="Limage">' . '<img src="images/colleges/'.$cImage.'"/>' . '</div>' .
'</td>' .
'</tr>';
}
}
mysqli_free_result($result);
}
mysqli_close($conn);
?>
</table>
The image portion in question is '<img src="images/colleges/'.$cImage.'"/>' . '</div>'.
A user could have up to 10 entries of college images, so i'd like to display these images side by side, rather than displayed in a different row with the same userid.
In other words, I'd like to group multiple rows of user data from the database in a single row with no duplicate userid in the table.
I've read about exploding the cImage and using foreach as well, but that's where my lack of experience gets me.
SELECT users.profileImage
, users.fname
, users.lname
, users.city
, users.state
, users.highschool
, users.primarysport
, group_concat(college.image) AS cImage
FROM usercollege
INNER JOIN users
ON users.id = usercollege.userid
INNER JOIN college
ON college.id = usercollege.collegeid
GROUP BY users.id
ORDER BY usercollege.createdate ASC
AND
foreach( explode(',', $row['cImage']) as $img )
echo '<div class="Limage">' . '<img src="images/colleges/'.$img.'"/>' . '</div>';
I want to display the last 3 news messages onto my PHP page. For that I am using the following code:
function news($number) {
$number = (int)$number
$query = mysql_query("SELECT `id`, `title`, `author`, `message`, `date`
FROM `news`
WHERE `hidden` = 0
ORDER BY `date`
DESC LIMIT $number");
while ($row = mysql_fetch_array($query))
{
return '<p class="p_sub">' . $row['title'] . '~' . $row['author'] .
'</p><p>' . $row['message'] . '</p>';
var_dump($row);
}
}
echo news(3);
However, this only displays one message, not three. Anyone who can figure out why?
Using return will exit your while loop. What you could do is concatenate a string containing your HTML like so
$html = '';
while ($row = mysql_fetch_array($query)) {
$html .= '<p class="p_sub">' . $row['title'] . '~' . $row['author'] . '</p><p>' . $row['message'] . '</p>';
}
return $html;
Is this possible somehow?:
Select all rows ( order by rand() )
Make a while loop that outputs all rows except the first one
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
while ($row = $stmt->fetch_assoc()) {
// IF NOT FIRST ROW, DO THIS
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
}
And then include the excluded row at end
$text .= '<p>' . $FIRSTROW_id . '<br />' . $FIRSTROW_name . '</p>';
Create a count and if it's first value, save it in variable.
Then, after loop, you use your variable with data from first row.
$sql = 'SELECT id, name FROM tablename ORDER BY rand ()';
$stmt = $conn->query($sql);
$i = 0;
while ($row = $stmt->fetch_assoc())
{
if ( $i == 0 )
$firstrow = $row;
else
$text .= '<p>' . $row['id'] . '<br />' . $row['name'] . '</p>';
$i++;
}
if ( $firstrow )
$text .= '<p>' . $firstrow['id'] . '<br />' . $firstrow['name'] . '</p>';
EDIT : From what you said in comments, you can just pass first row as param in AJAX and exclude it in your query :
$sql = "SELECT id, name FROM tablename WHERE id != '".intval($_GET['id'])."' ORDER BY rand ()";
$stmt = $conn->query($sql);
$_GET['id'] will be param you send by AJAX.
So let's say I have 30 entries in my database, I select the first 7 to get on my first page like this:
$qryRandomGallery = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
FROM tblKWKids AS A
LEFT JOIN tblScore AS b
ON a.ScoreID = b.ScoreID
LEFT JOIN tblUser as C
ON a.UserID = c.UserID
ORDER BY RAND()
LIMIT 6";
if ($stmtRandomGallery = mysqli_prepare($dbconn, $qryRandomGallery)) {
mysqli_stmt_execute($stmtRandomGallery);
mysqli_stmt_bind_result($stmtRandomGallery, $KWTitel, $KWURL, $KWID, $KWKiddyBeschrijving, $ScoreAfb, $USER);
mysqli_stmt_store_result($stmtRandomGallery);
}
$qryRandomGalleryBIG = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
FROM tblKWKids AS A
LEFT JOIN tblScore AS b
ON a.ScoreID = b.ScoreID
LEFT JOIN tblUser as C
ON a.UserID = c.UserID
ORDER BY RAND()
LIMIT 1";
if ($stmtRandomGalleryBIG = mysqli_prepare($dbconn, $qryRandomGalleryBIG)) {
mysqli_stmt_execute($stmtRandomGalleryBIG);
mysqli_stmt_bind_result($stmtRandomGalleryBIG, $KWTitelB, $KWURLB, $KWIDB, $KWKiddyBeschrijvingB, $ScoreAfbB, $USERB);
mysqli_stmt_store_result($stmtRandomGalleryBIG);
}
and then this as my php
while(mysqli_stmt_fetch($stmtRandomGallery)){
$content .= '<div>';
$content .= '<a href="galerij.php?id=' . $KWID . '">';
$content .= '<img src="' . $KWURL . '" title="' . $KWTitel . '" alt="' . $KWTitel . '" class="image">';
$content .= '<h5>' . $KWTitel . ' door: ' . $USER . '</h5>';
$content .= '<p>' . $KWKiddyBeschrijving . '</p>';
$content .= '<img src="' . $ScoreAfb . '" title="Score" alt="Score" class="img">';
$content .= '</a>';
$content .= '</div>';
}
while(mysqli_stmt_fetch($stmtRandomGalleryBIG)){
$content .= '<h2>Uitgelicht werk van ' . $USERB . '</h2>';
$content .= '<a href="galerij.php?id=' . $KWIDB . '">';
$content .= '<img src="' . $KWURLB . '" title="' . $KWTitelB . '" alt="' . $KWTitelB . '" id="image">';
$content .= '<h3>' . $KWTitelB . ' door: ' . $USERB . '</h3>';
$content .= '<h4>' . $KWKiddyBeschrijvingB . '</h4>';
$content .= '<img src="' . $ScoreAfbB . '" title="Score" alt="Score" id="score">';
$content .= '</a>';
}
Now, how do I exclude the results from the first stmt to select another random image for my BIG image?
Simple: Take the IDs of your first query and stuff them into the second query as a not in:
SELECT ...
FROM yourtable
WHERE idField NOT IN (x,y,z,p,q,r)
This'll exclude them from second query's results.
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}