I'm lost on MySQLi. Manages a blog on standard mysql query, MySqli threw me. Followed a tutorial for inserting into a database. Notice the link for Edit. Not sure how to jump to a edit page to update. It this the wrong way of doing it? I think it's the echo that is the echo that is the issue?
<?php
$sql = "
SELECT snippets.Title, snippets.Link, snippets.Text, snippets.Created, camp_names.ribbon as Ribbon, camp_names.alt_text, camp_names.name as Campaign, camp_names.id
FROM snippets
LEFT JOIN camp_names ON snippets.Campaign = camp_names.id
ORDER BY camp_names.id ASC
";
$results = $db->query($sql);
if($results->num_rows) {
While($row = $results->fetch_object()) {
echo "
<div class='snippets'>
<div class='title'><h5><a href=''>{$row->Campaign}</a></h5><strong>{$row->Title}</strong> - Created:{$row->Created}</div>
<div class='ribbon_wrapper'>
<div class='ribbon'><img src='{$row->Ribbon}' alt='{$row->alt_text}' /></div>
<div class='ribbon_text'>{$row->Text}...<a href='{$row->Link}'> Read more</a></div>
<a href='#'>edit</a>
</div>
</div>
";
}
} else {
echo 'No Results';
}
?>
Related
so, I'll try to explain it as good as I can with my knowledge in english.
I am trying to count data from my database, basically - how many fields I have with the same ID. here's image from database.
image from database
for example, in posts i have review system and it works, i have this PHP code to count how many same fields i have with review and id.
Here's the code:
<?php
$revposid = $res['content_id'];
$pos="SELECT review FROM comments WHERE review='positive' and postid=$revposid";
$neg="SELECT review FROM comments WHERE review='negative' and postid=$revposid";
$neu="SELECT review FROM comments WHERE review='neutral' and postid=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neg)){$rowcountneg=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neu)){$rowcountneu=mysqli_num_rows($result);}
?>
<div class="reviews" id="reviews">
<span class="good"><b class="fa fa-thumbs-up"></b><?php echo $rowcountpos ?></span>
<span class="neutral"><b class="icon-thumbs-up"></b><?php echo $rowcountneu ?></span>
<span class="bad"><b class="fa fa-thumbs-down"></b><?php echo $rowcountneg ?></span>
</div>
and when I try to use the same code
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
in my other script I have like system, it should show all my likes and under likes total likes of the post but when I use it it shows unlimited amount of data, i have no idea why. Here's the full code, I would appreciate some help or explanation.
<?php $ususername = $_GET['user_username'];$sql = "SELECT * FROM user_details WHERE user_username='$ususername'";$usresult = mysqli_query($_db,$sql);?>
<?php if( ! mysqli_num_rows($usresult) ) {
echo " Ooops? <br> <br>User <b>".$_GET["user_username"]."</b> doesn't exist.";
} else {
while($usrow = mysqli_fetch_array($usresult,MYSQLI_BOTH)) {?>
<?php
$current_user = $usrow['user_id'];
if ($_db->connect_error) {
die("Connection failed: " . $_db->connect_error);
}
$sql = "SELECT * FROM user_content_like WHERE user_id=$current_user ORDER BY date_added DESC;";
$result = $_db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$content_id = $row['content_id'];
$sql1 = "SELECT * FROM content WHERE content_id=$content_id";
$result1 = $_db->query($sql1);
if ($_SESSION['user_id'] == $usrow['user_id']) {$output = '
<button type="button" class="unlike_button" onclick="unlike(this);" name="like_button" data-content_id="'.$row["content_id"].'" ><i class="fa fa-minus"></i></button>
';} else {
$output = '';
}
while($cont = $result1->fetch_assoc()) {
$revposid = $cont['content_id'];
$pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
echo '
<div class="community-feed-thread">
<div class="community-icon-thread"></div>
<div class="community-comments-thread">'.$output.'</div>
<a href="'.$cont["content_id"].'" class="community-title-thread"><h3>'.$cont["title"].'</h3>
<span class="likes-desc"> Total likes: '.$rowcountpos.'</span>
</a>
</div>
';
}
}}
else {
echo " hmmmmmmmmmmmm.<Br><br>". $usrow["user_username"]." doesn't like anything. ";
}
$_db->close();
?>
<?php }}?>
this is how i want it to look
This is how it looks
Your re-using the same variable for different result sets in the code...
$result = $_db->query($sql);
and
if ($result=mysqli_query($_db,$pos))
You will need to ensure you only use the variable name once or it may have side effects in other loops.
Hi Friends
The code I've presented displays data (moduleID & moduleName) associated with a log in user. Presently it shows specific html code associated with a moduleID that should be displayed on the screen when the user logs in. I want to implement a way to iterate through the values (Possibly if-else statement within loop) in order the specific html code i have presented to be shown when a user has one or many moduleID's attached to them.
Here is my homepageDemo.php
<?php
session_start();
//including the database connection file
include 'config.php';
$cuserID= $_SESSION['userID'];
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$cuserID'"); // using mysqli_query instead
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4439Function()'>IS4437</a></p>
</div>
";
}
?>
Here is the html code attached to the moduleID of IS4437
<div id="loader-wrapper">
<div id="loader"><div id="loader1">
</div>
</div>
<p><a onclick="IS4439Function()">IS4437</a></p>
</div>
Here is the html code attached to the moduleID of IS4408
<div id="loader-wrapper1">
<div id="loader"><div id="loader1">
</div>
</div>
<p><a onclick="IS4408Function()">IS4408</a></p>
</div>
EDIT
So as I've mentioned the problem i'm currently having, these are the different methods I've already tried.
Originally before embedding the html into the while loop in the homepageDemo.PHP, I simply returned the moduleID & moduleName associated with logged in user as text
<?php
session_start();
//including the database connection file
include 'config.php';
$cuserID= $_SESSION['userID'];
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT m.moduleID, m.moduleName
FROM modules m
JOIN courses c ON m.courseID = c.courseID
JOIN usersDemo u ON c.courseID = u.courseID
WHERE userID = '$cuserID'"); // using mysqli_query instead
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"."<a>".$res['moduleID']."</a>"."</td>";
echo "<td>"."<a>".$res['moduleName']."</a>"."</td>";
}
?>
Then I attempted to iterate through the values to return the html code associated with each individual moduleID
SWITCH STATEMENT ATTEMPT:
while($res = mysqli_fetch_array($result)) {
switch ($cuserID) {
case 1:
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4439Function()'>IS4437</a></p>
</div>
";
break;
case 2:
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='IS4408Function()'>IS4408</a></p>
</div>
";
break;
case 7:
echo"
<div id='loader-wrapper'>
Functions called
function IS4439Function() {
{
window.location.href = "global.php";
}
}
function IS4408Function() {
{
window.location.href = "isStrategy.php";
}
}
function IS4449Function() {
{
window.location.href = "webApp.php";
}
}
Unfortunately neither were what I was trying to achieve. Any thoughts?
You're really close, and I'm not exactly sure where the disconnect is, but here you go. You need to replace the hardcoded moduleID value (IS4439) with the value that you've already pull from the db. Putting together what you already have, it really just comes down to concatenation.
while($res = mysqli_fetch_array($result)) {
echo "
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='" . $res['moduleID'] . "Function()'>". $res['moduleID'] . "</a></p>
</div>
";
}
I would suggest though, that instead of calling different functions, you consider calling just one function, and passing in the moduleID as a parameter and then act accordingly within the function.
Based on your update, you would then have this...
while($res = mysqli_fetch_array($result)) {
?>
<div id='loader-wrapper'>
<div id='loader'><div id='loader1'>
</div>
</div>
<p><a onclick='redirectFunction("<?php echo $res['moduleID']; ?>");'><?php echo $res['moduleID']; ?></a></p>
</div>
<?php
}
?>
and your function would be...
function redirectFunction(moduleID)
{
var newLocation = "";
if(moduleID == "IS4439")
{
newLocation = "global.php";
}
else if(moduleID == "IS4408")
{
newLocation = "isStrategy.php";
}
else if(moduleID == "IS4449")
{
newLocation = "webApp.php";
}
window.location.href = newLocation;
}
I am currently working on developing a PHP website, and I would like to add a section to the home page that displays news headlines. The information to populate this section, like all other content on the site, will be pulled from a MySQL database. I have added the code that will let me do this and a little CSS before each part to format the display on the page and that works wonderfully. But, what I cannot work out is how to display all the content I want and insert a horizontal rule after each MySQL row is read. Right now, if I insert more than one row into the database table, it only displays the information from the new row and replaces the info from the previous. Refer to the image below to see how the page currently appears.
Below is the code that I am using.
<div id="news">
<?php
$query = "SELECT news_date FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$date = $row['news_date'];
}
?>
<?php
$query = "SELECT news_headline FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$headline = $row['news_headline'];
}
?>
<?php
$query = "SELECT news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) {
$body = $row['news_body'];
}
?>
<div class="date"><?php echo $date . "<br />" ?>
<div class="headline"><?php echo $headline . "<br />" ?>
<div class="headlinebody"><?php echo $body . "<hr />" ?>
</div>
As I've said, this will display the info the way I want it, but as soon as I add another "headline", the new replaces the old. I would like for all headlines to appear with a horizontal rule between the headlines (rows) and not each part. Any help you can provide would be appreciated. Thank you much in advance.
You can do this in 1 query and 1 loop.
<div id="news">
<?php
$query = "SELECT news_date, news_headline, news_body FROM news";
$result = mysqli_query($dbconnect, $query);
while($row=mysqli_fetch_assoc($result)) { ?>
<div class="date"><?php echo $row['news_date']; ?></div><br />
<div class="headline"><?php echo $row['news_headline']; ?></div><br />
<div class="headlinebody"><?php echo $row['news_body']; ?></div><hr />
<?php }
?>
</div>
I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections
I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());