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.
Related
hi am trying to update multiple column , how can i? the first column is shoe1, wanna add another column but cant
// shoe1
$query = "SELECT shoe_id, shoes_image,
shoes FROM " . $DBPrefix . "shoes";
$params[] = array(':shoe_id', floor($time_passed), 'int');
$db->query($query, $params);
$TPL_shoe1_list = '<select name="shoe1">' . "\n";
while ($row = $db->fetch())
{
$selected = ($row['shoes'] == $shoe1) ? 'selected="true"' : '';
$TPL_shoe1_list .= "\t" . '<option value="' . $row['shoes'] . '" ' . $selected . '>' . $row['shoes'] .'</option>' . "\n";
}
$TPL_shoe1_list .= '</select>' . "\n";
In General the sql syntax is
UPDATE table-name
SET column-name = value, column-name = value, ...
WHERE condition
Example:
UPDATE Supplier
SET City = 'Oslo', Phone = '(0)1-953530', Fax = '(0)1-953555'
WHERE Id = 15
I have a table with 7 entries and in future there will be added more.
I need to insert an image after every 3rd result from table.
Currently it would be 2 images, but later it can be more.
$sqlSelect = "SELECT name,rank,points FROM `users` WHERE rank = 1";
$data = $db->query($sqlSelect);
foreach ($data as $row) {
$name = $row['name'];
$rank = $row['rank'];
$pts = $row['points'];
echo '<a href="/' . $name . '" title="' . $name . '">;
echo $name . ' | ' . $rank . ' | ' . $pts . '</a>';
}
Would this work?
count(*) AS count
$cnt = $row['count'];
Then put that into for loop. But I can't figure out what do I write in the for loop.
for ($x = 3; $x == $cnt; $x++) {
if ($x == 3) {
echo "The number is: $x <br/>";
}
}
How do I add +3 to the X instead of X++ ?
Problem
I need to insert an image after every 3rd result from table.
Solution
You can use a simple variable, like $counter to display an image after every third row, like this:
<?php
$sqlSelect = "SELECT name,rank,points FROM `users` WHERE rank = 1";
$data = $db->query($sqlSelect);
$counter = 1; // to keep track of number of rows
foreach ($data as $row) {
if($counter % 4 == 0){
// display image here
}
++$counter;
$name = $row['name'];
$rank = $row['rank'];
$pts = $row['points'];
$path = '<a href="/' . $name . '" title="' . $name . '">';
$path .= $name . ' | ' . $rank . ' | ' . $pts . '</a>';
echo $path;
}
?>
And it'll be an overkill to execute a separate query to count the number of rows.
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;
I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}
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>';