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.
Related
Help don't know how to call the name of the position, My problem is my codition because when I change it to a position like 'Commissioner' it won't work but when I change to its id it will work but I want that my codition will be the name of the position, Please need help how to change this to a name: $dsds mean the id of the position
<?php
include ('../connection/connect.php');
$result = $db->prepare("SELECT * FROM candposition ORDER BY posid ASC");
$result->bindParam(':userid', $res);
$result->execute();
for ($i = 0; $row = $result->fetch(); $i++) {
$dsds = $row['posid'];
for ($i = 0; $rows = $results->fetch(); $i++) {
if ($dsds == '9012') {
$result = $db->prepare("SELECT * FROM program ORDER BY progid ASC");
echo $rows['progid'];
echo '<br />';
echo '<div style = "margin-left:2px;display:inline-block;position:relative;">';
echo '<img src="candidates/images/' . $rows['image'] . '" width="90" height="100px" />' . ', ' . '<br />' . $rows['lastname'] . ', ' . $rows['firstname'] . '<br />' . ' = ' . $rows['votes'];
echo '<br />';
}
}
}
instead of : if ($dsds == '9012'){ add : if ($row[1]== 'Commissioner')
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>';
}
}
}
There is two rows that should be printed from my sql.
id Symbol Shares
3 CSCO 40
3 FB 200
but this code prints only first row twice. Like this:
jharvard has 40 shares of CSCOjharvard has 40 shares of CSCO
Why?
Thanky you in an advance.
$users = query("SELECT * FROM users WHERE id = ?", $_SESSION["id"]);
$rows = query("SELECT * FROM stocks WHERE id = ?", $_SESSION["id"]);
$stock = $rows[0];
$username = $users[0];
foreach ($rows as $row)
{
if( $stock["id"] === $_SESSION["id"])
{
print("<td>" . $username["username"] . " has " . $stock["Shares"] . " shares of " . $stock["Symbol"] . "</td>");
}
}
The reason is your variables inside the loop
foreach ($rows as $row) {
if( $stock["id"] === $_SESSION["id"]) {
print("<td>" . $username["username"] . " has " . $stock["Shares"] . " shares of " . $stock["Symbol"] . "</td>");
}
}
You are printing the value of $stock, which earlier you defined:
$stock = $row[0];
So it will always output the data from the first row.
I suspect you want to change your loop as follows:
foreach ($rows as $row) {
// Changed from $stock['id'] to $row['id']
if( $row["id"] === $_SESSION["id"]) {
// Changed from $stock['Shares'] and ['Symbol'] to $row['Shares'] and ['Symbol']
print("<td>" . $username["username"] . " has " . $row["Shares"] . " shares of " . $row["Symbol"] . "</td>");
}
}
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.
I need a little help here with some php.
just a little explanation:
(im trying to display 5 results using this code
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
} else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
It does display but i want that 1st result to be (image+title of the news and other results to be just title).
Hope i wrote it clearly what i needed help with.
Thank you
Your SQL statement is only fetching a single row. This isn't a complete solution, but should get you closer:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$n_id = (int)$_GET['id'];
$path = '';
$count = 0;
$sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
$news = mysql_query($sql);
while ($result = mysql_fetch_array($news)) {
$title = $result['title'];
$date = $result['date'];
echo '<b>' . $title . ' | ' . $date . '</b>';
if ($count < 1) {
echo '<br/><img src="images.php?id='. $n_id .'>';
$count++;
}
}
if ($count == 0) { header("Location: vendi.php"); }
echo '<br />Back to Archive';
}
This should do it.
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
$first = TRUE;
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
if($first == TRUE) {
echo '<b>' . $title . ' | ' . $date . '</b>';
$first = FALSE;
}
else {
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
}
}
else {
header("Location: vendi.php");
}
echo '<br />Back to Archive';
}
}
While looping through your results, you can check if you are at the first result:
$counter = 0;
while ($row = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>';
if ( $counter == 0 ) {
echo '<br /><img src="images.php?id='. $n_id .'>';
}
}