how to call the name instead of id? - php

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

Related

Display comments for each post in PHP/mySQL site

I'm trying to display multiple posts, each with its own multiple comments. This, below, works for the first post (or, at least shows 2 of the 3 comments), but not for the rest. I searched but couldn't find (or didn't recognize) an answer to this exact problem on this site, or elsewhere. Thanks.
$result1 = #mysql_query('select * from posts,comments where comment_post_ID = ID and post_status="publish" and comment_approved="1" ORDER BY post_date,category,subcat1,subcat2 ASC');
$result2 = #mysql_query('select * from comments where comment_approved="1" ORDER BY comment_date');
$row = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);
while ($row = mysql_fetch_array($result1)) {
$post = $row['post_content'];
$id = $row['ID'];
$title = $row['post_title'];
$content = "<br /><h3 align=\"left\">" . $title . "</h3>\n";
$content .= "<h4 align=\"left\">by " . $row['post_author'] . " - " . $row['post_date'] . "</h4>\n";
$content .= "<p align=\"left\">" . var_export($post, true) . "</p>\n<p>\n";
echo $content;
while ($row2 = mysql_fetch_array($result2)) {
$comment = "<blockquote>";
$comment .= "<h4 align=\"left\">" . $row2['comment_author'] . " commented on " . $row2['comment_date'] . "</h4>\n";
$comment .= "<p align=\"left\">" . $row2['comment_content'] . "</p>\n<p>\n";
$comment .= "</blockquote>";
if($row2['comment_post_ID'] == $id) {
echo $comment;
}
}
}
This, below, finally worked as hoped:
$postData = mysqli_query($conn,"SELECT * FROM wp_posts ORDER BY post_date ASC) or die(mysqli_error());
$commentData = mysqli_query($conn,"SELECT * FROM wp_comments WHERE comment_approved = '1' ORDER BY comment_date ASC") or die(mysqli_error());
$posts = array();
while($row = mysqli_fetch_assoc($postData)) {
$posts[] = $row;
$commentrow = mysqli_fetch_assoc($commentData);
$comments[] = $commentrow;
echo '<h3>' . $row['post_title'] . '</h3>';
echo '<h5>' . $row['post_author'] . ', ' . $row['post_date'] . '</h5>';
echo '<p>' . $row['post_excerpt'] . '... read more</p>';
if($row['comment_count'] > 0) {
echo '<blockquote>';
echo '<b>Comments</b><br />';
foreach($comments as $comment) {
if($row['ID']==$comment['comment_post_ID']) {
$comment_excerpt = substr($comment['comment_content'],0,100);
echo '<br>' . $comment_excerpt . ' - <b>' . $comment['comment_author'] . '</b>, ' . $comment['comment_date'] . '<br>';
}
}
echo '</blockquote>';
}
}

php insert image after every 3rd result

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.

Sorting nested While() Loops

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>';
}
}
}

Order by rand(). Put first row in the end

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.

Extraction of the data from MySQL using PHP

$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'];
}
}

Categories