I have some php code that looks like :
echo"
<tr>
<td colspan=\"2\">" . get_comment(${'imgmodp_data_' . $i}['primid']) . "</td>
</tr>
";
My get_comment function:
function get_comment($primid)
{
$dbCon = mysqli_connect("localhost", "something", "something", "something");
$sql = "SELECT `id_sheep`, `text` FROM `comments` WHERE `primid`='$primid'";
$query = mysqli_query($dbCon, $sql);
while($row = mysqli_fetch_array($query))
{
return = "<div>" . $row['text'] . "</div></br>";
}
}
But this only returns one row each time even if they is multiple rows. If I use echo instead of return it returns all lines but at the top of my document, not in the blocks where it should be...
Any suggestions?
Use a variable before your loop, which you update inside your loop and return after your loop.
If you just use return, you'll stop the function, so after 1 loop.
With echo you output the string directly, but don't stop the loop.
For example:
function get_comment($primid){
$dbCon = mysqli_connect("localhost", "something", "something", "something");
$sql = "SELECT `id_sheep`, `text` FROM `comments` WHERE `primid`='$primid'";
$query = mysqli_query($dbCon, $sql);
$return = '';
while($row = mysqli_fetch_array($query)){
$return .= "<div>" . $row['text'] . "</div></br>";
}
return $return;
}
What about this
echo "<div>" . $row['text'] . "</div></br>";
and place the php code where you want it to show.
<div class="row">
<div class="col-sm-12">
<div class="col-sm-6">
<!-- PHP code goes here -->
</div>
</div>
</div>
Related
I am relatively new to PHP and want to know a more efficient way of echo'ing the following in less steps or even just one statement.
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
echo "</p></div>";
As #droopsnoot has commented on above, you should try doing the next thing here.
while( $row = $result->fetch_assoc()) {
echo "<div class='card'><p class: card-text>". $row['uid']."<br>" . $row['date']."<br>".$row['message']."<br><br>". "</p></div>";
}
It should work perfectly.
Another option is to close php tag (?>) and use html markup with <?=$var?>:
<?php
$sql = "SELECT * FROM comments";
$result = OpenCon()->query($sql);
while( $row = $result->fetch_assoc()) {?>
<div class="card">
<p class="card-text">
<?=$row['uid']?><br>
<?=$row['date']?><br>
<?=$row['message']?><br><br>
</p>
</div>
<?php
} // end while
I have a question about PHP/MYSQL.
Here you see my php page in the browser:
And here you see my phpmyadmin database:
What I want is when I click on:"Laders" I only want to see on the next page everything in the "groep":"Laders". The problem is I don't know how to do that in a loop. Here is the code:
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'producten');
$query = "SELECT *
FROM producten
WHERE Merk = '" . 'Apple' . "'
ORDER BY Artikelnummer, Artikelnaam";
/*echo $query;*/
$result = mysqli_query($link, $query);
?>
<table>
<?php
echo '<table style="width:100%">
<tr style="color:yellow; background-color:black;">
<th>Apple</th>
</tr>';
foreach ($result AS $rij)
{
echo '<tr style="background:blue"><td>' . $rij['Groep'] . ' </td></tr>';
}
?> </table>
foreach ($result AS $rij)
{
echo '<tr style="background:blue"><td>'.$rij['Groep'].'</td></tr>';
}
After this, create a php file "view.php" and fetch data using $_GET['variable']
I'm in need of some help. How can I output the nl2br() and htmlentities() functions into my code? Both need to be applied to 'content' but htmlentities to 'title'.
<?php
$sql = "
SELECT
post.id AS postid, post.title AS title, post.created,
DATE_FORMAT(Created, '%d-%m-%Y') AS created,
post.content AS content, post.image AS image,
post.alttext AS alttext, blog.id, blog.blogname
FROM
post
LEFT JOIN
blog ON post.blogid = blog.id
WHERE
blogid = \"" .(int)$_GET['id'] . "\"
ORDER BY
postid DESC";
$results = $db->query($sql);
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>{$row->title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$row->content}
</p>";
}
} else {
echo 'No Results';
}
?>
You can use a new variable for that:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$title = htmlentities($row->title);
$content = nl2br(htmlentities($row->content));
echo "<hr>
<h3>{$title}</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
{$content}
</p>
";
}
}
Or break the string:
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
echo "<hr>
<h3>". htmlentities($row->title) ."</h3>
<p>{$row->created}</p>
<p>
<div id='blog-image'>
<img src='images/{$row->image}' alt='{$row->alttext}'>
</div>
". nl2br(htmlentities($row->content)) ."
</p>
";
}
}
The neatest way is to abstract the fetch process, so that the fact that data needs demangling, is abstracted away from the view process which draws the result.
Add a function :-
function demangle_row($row) {
$row->content = nl2br($row->content);
$row->content = htmlentities($row->content);
$row->title = htmlentities($row->title);
}
Now change the while statement, to use the above code so
While($row = $results->fetch_object()) {
becomes
While($row = demangle_row($results->fetch_object())) {
I am doing a project and would be eternally grateful for help in getting my URl's to link. I have tried looking around to no avail. I have a database (4columns). The last one (link1) should link to videos with the specified URL.When the table comes up the URL's are not clickable (is there a way to simplify this say "click me"?). Here is my code. I've also attached an image of the table. This is really busting my brains, thanks.
<?php
$con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Table output
Try this:
<?php
$sql = "SELECT * FROM `videos`";
$result = mysqli_query($con, $sql);
?>
<table>
<?php
while($row = mysqli_fetch_assoc( $result)) {
?>
<tr>
<td><?php echo $row['topic1'];?></td>
<td><?Php echo $row['subject1'];?></td>
<td><a href="<?php echo $row['link1']; ?>" target="_blank">Click me</td>
</tr>
<?php } ?>
<table>
Or you can also use do while loop:
do{
echo '<tr>';
echo '<td>'.$row['topic1'].'</td>';
echo '<td>'.$row['subject1'].'</td>';
echo '<td><a href="'.$row['link1'].'" target="_blank">Click me</td>';
echo '</tr>';
} while($row = mysqli_fetch_assoc( $result);
I added the target attribute to open the link in a new window.
I looked at your code and i found a couple errors.
change $con = mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test"); to $con = new mysqli("localhost", "feedb933_charles", "pass100", "feedb933_test");
Then change if (mysqli_connect_errno()) to if (mysqli_connect_error()) {
Then change
$sql = "SELECT * FROM videos";
to
$sql = "SELECT topic1, subject1, link1 FROM videos";
or if you want to select one row
$differentvalue = ""; // value to run
$sql = "SELECT topic1, subject1, link1 FROM videos WHERE difvalue = ?";
difvalue is the value different frm the rest so the php code knows what to grab.
Using stmt means no sql injection
Add:
$stmt = $con->stmt_init();
if (!$stmt->prepare($sql))
{
print "Failed to prepare statement\n";
}
else
{
}
Then in side if (something) { } else { IN HERE }
(if you have WHERE diffvalue) Add:
$stmt->bind_param("s", $differentvalue); // $stmt->bind_param("s", $differentvalue); if text, $stmt->bind_param("i", $differentvalue); if integer
Then
Add:
$stmt->execute();
$list = $stmt->fetchAll();
then outside the if (something) { code } else { code }
Add:
echo "<table><tr><th>topic1</th><th>subject1</th><th>link1</th></tr><tr>";
foreach ($list as $row => $new) {
$html = '<td>' . $new['topic1'] . '</td>';
$html .= '<td>' . $new['subject1'] . '</td>';
$html .= '<td>' . $new['link1'] . '</td>';
echo $html;
}
echo "</tr></table>";
If you are still having problems goto the links listed
http://php.net/manual/en/pdostatement.fetchall.php
http://php.net/manual/en/mysqli-stmt.get-result.php
http://php.net/manual/en/mysqli-result.fetch-all.php
Hope this helps
<?php
$con=mysqli_connect("localhost","feedb933_charles","pass100","feedb933_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM videos";
$result = mysqli_query($con, $sql);
echo "<table>";
echo "<tr>
<th>topic1</th>
<th>subject1</th>
<th>link1</th>
</tr>";
while( $row = mysqli_fetch_array( $result)) {
$topic1 = $row["topic1"];
$subject1 = $row["subject1"];
$link1 = $row["link1"];
echo "<tr>
<td>$topic1</td>
<td>$subject1</td>
<td>$link1</td>
*//this href will give u the link as u asked. //be sure you store proper url. In case of exact video name saved in column thn can make the url for your web output. //ex:<a href="http://example.com/'.$link.'.extension">*
</tr>";
}
echo "</table>";
mysqli_close($con);
?>
We have a basic PHP script to extract the title and description for each job from a MySQL database as simply display this information. This is what it looks like:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
$results = mysql_fetch_assoc($query);
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
Now, this only extracts one row from the database, but it should extract two. So, I tried the following to replace the while statement:
<?php foreach($results as $result) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
This statement doesn't work either. This just displays (weirdly) the first character of each column in the first row in the table.
Does anyone have any idea as to why this isn't working as it should?
In your while use same variable $result as you started:
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
and remove the first $results = mysql_fetch_assoc($query);
Result variable you have used is result not results
Replace
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
**$results = mysql_fetch_assoc($query);** // remove this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$results['title']}</h2>";
echo "<p>{$results['desc']}</p>";
echo '</div>';
} ?>
to
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You already fetched the first row before your loop started, which is why it only prints the second row. Simply comment out that line:
#$results = mysql_fetch_assoc($query); # here is your first row,
# simply comment this line
<?php while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
} ?>
You are also looping over $result but using $results in your while loop body.
Check this:
$sql = "SELECT `title`, `desc` FROM jobs WHERE active = 'y'";
$query = mysql_query($sql) or die('<em><strong>SQL Error:</strong> ' . mysql_error() . '</em>');
<?php
while($result = mysql_fetch_assoc($query)) {
echo '<div class="left_content" style="margin-top: 15px;">';
echo "<h2>{$result['title']}</h2>";
echo "<p>{$result['desc']}</p>";
echo '</div>';
}
?>
Change this line
<?php while($result = mysql_fetch_assoc($query)) {
to
<?php while($results = mysql_fetch_assoc($query)) {