PHP Display Data In Horizontal Style From MySql - php

I am trying to display all my site members info with avatar and some information in members page of my WordPress site.For this, I have written following code.It is displaying data with one single user in a row.What I am trying is to display multiple users like 4 to 5 members horizontally in a single row then start 2nd row similar like this https://stackoverflow.com/users
Following is my code.how can achieve this
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
foreach($result as $row)
{
echo '<table><tr>';
echo '<td>'.get_avatar( $row->id,40 );
echo '</td><td>'.$row->id." ".$row->pt. "<br>" .$row->re. "</td></tr>
</table>";
}
?>

I prefer to solve the problem by using an count and define a fixed number of columns.
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id"); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
$count = count($result);
$columns = 5;
echo '<table><tr>';
foreach($result as $i => $row) {
echo '<td>' . get_avatar( $row->id,40 ) . '</td>';
echo '<td>' . $row->id . ' ' . $row->pt . '<br>' . $row->re . '</td>';
$i++;
if($i != $count && $i >= $columns && $i % $columns == 0)
echo '</tr><tr>';
}
echo '</tr></table>';
?>

Try with this,
<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT id,display_name as pt,user_registered as re FROM wp_users group by id");
?>
<div class="container">
<?php foreach($result as $row) { ?>
<div class="row">
<div class="col-lg-3">
<?php get_avatar( $row->id,40 ); ?><br>
<?php echo $row->id." ".$row->pt;?><br>
<?php echo $row->re;?>
</div>
</div>
<?php } ?>
</div>

Related

Two articles in my php array

I'm trying to make a webshop for an assignment and I can't figure out how to display that there are 2 of the same product in my checkout. This is my code I want to display something like: Article1 .. 2x Article2 ... 5x etc.
<?php
session_start();
//read out session with article numbers
$array = $_SESSION['mandje'];
echo '<center>';
echo '<h1> Uw mandje: </h1>';
echo '<table style="border: 2px solid black">';
//array that checks if there are two of the same article numbers
$mandje = array();
//read out array
foreach ($array as $artikel)
{
echo '</br>';
echo $artikel;
if (in_array($artikel, $mandje)){
//I need to display the article that i have multiple times here i guess
} else {
//getting the articles out of the database
require ('config.php');
$query = "SELECT * FROM mphp6_meubels WHERE artikelnr = $artikel";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '</tr>';
}
}
//adding the article to array so i can check if there are multiple of the same articles in array
$mandje[] = $artikel;
}
echo '</table>';
echo '</center>';
Just group by artikelnr field.
foreach ($array as $artikel)
{
require ('config.php');
$query = "SELECT mphp6_meubels.*, count(*) as cnt FROM mphp6_meubels WHERE artikelnr = $artikel GROUP BY artikelnr";
$results = mysqli_query($mysqli, $query);
while($meubel = mysqli_fetch_array($results)){
$video = $meubel['naam'];
$nmr = $meubel['artikelnr'];
echo '<tr>';
echo '<td>' . $artikel . '</td>';
echo '<td> <img src="Meubels/'.$video.'.jpg" width="150" height="150" alt="Meubel"></td>';
echo '<td> ' . $meubel['cnt'] . '</td>';
echo '</tr>';
}
}

passing variable to another page for sql query

I am trying to pass a variable from a page to another page so that i can display more information on the other page by getting the idea. I am not really sure what is going wrong and because I use bluemix it doesn`t display an error it just gives me the page 500 error. Here is my code:
<?php
$strsql = "select id, name, problem, urgency, technology from idea WHERE status='saved'";
if ($result = $mysqli->query($strsql)) {
// printf("<br>Select returned %d rows.\n", $result->num_rows);
} else {
//Could be many reasons, but most likely the table isn't created yet. init.php will create the table.
echo "<b>Can't query the database, did you <a href = init.php>Create the table</a> yet?</b>";
}
?>
<?php
echo "<tr>\n";
while ($property = mysqli_fetch_field($result)) {
echo '<th>' . $property->name . "</th>\n"; //the headings
}
echo "</tr>\n";
while ( $row = mysqli_fetch_row ( $result ) ) {
$idea_id= $row['id'];
echo "<tr>\n";
for($i = 0; $i < mysqli_num_fields ( $result ); $i ++) {
echo '<td>' . "$row[$i]" . '</td>';
}
echo "</tr>\n";
}
$result->close();
mysqli_close();
?>
There's a small bug in your code. Your echo <a href ="... line should be like this,
echo '<td>' . $row[$i] . '</td>';

php & pdo & mysql countrows returning 0

I have now spent several hours without getting anywhere.
I want to display number of rows in my query and here here is my code:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rResult = $statement->fetchAll();
echo count($rResult);
echo "<h2>Interesi</h2>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And i getting number 10 but now im not getting any interests displayed.
If you use this code now:
<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And now im getting all the interests displayed but it counts: 0 interests.
Any ideas who i can count number of interests?
If you read the documentation for PDOStatement::rowCount you will see this line
If the last SQL statement executed by the associated PDOStatement was
a SELECT statement, some databases may return the number of rows
returned by that statement. However, this behaviour is not
guaranteed for all databases and should not be relied on for portable
applications.
The work around is to count() function from php like you did. If that did not work then use COUNT(*) as result_count. then when you fetch read that value.
And i getting number 10 but now im not getting any interests displayed
problem
The reason why the result aren't displaying is because you fetched all the row already so you cannot use fetch() again.
solution
Use the array returned by fetchAll() instead and use a foreach instead
$rows = $statement->fetchAll();
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
//echo $row['name']
}
<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
$counter = 0; //declare counter
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
$counter++; //increment counter
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
[NOTE: Edit by #DinoAmino removed the emphasis characters around the two uses of $counter, which caused the code to not compile for the question asker]
I solved my problem like this:
Here is the updated solving version. Thank you all for the input:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rows = $statement->fetchAll();
if(count($rows) >= '1') {
?>
<div class="your-interests">
<?php
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
</div>
<?php } ?>

Echoing multiple rows of a database query with php

I am trying to make an accordion (twitter bootstrap) displaying all of someone's friends, I end up just creating a infinate loop, basically all I am trying to do is echoing out a php string for each line in the results.
can anyone spot what I am doing stupid (other than not using mysqli)?
while ( $rowresult = mysql_fetch_assoc(mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ")) ) {
$auto = "0px";
echo '<div class="accordion-group"><div class="accordion-heading"><div class="accordion-heading">';
echo '<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse'. $counter . '">';
echo $rowresult['firstname'];
echo ' ';
echo $rowresult['lastname'];
echo ', ';
echo $rowresult['headline'];
echo '</a></div> <div id="collapse'. $counter .'" class="accordion-body ';
if ($counter == 0){
echo 'in ';
$auto = "auto";
}
echo 'collapse" style="height: '. $auto .'; "><div class="accordion-inner">';
echo '<img src="'.$rowresult["pictureUrl"].'">';
echo '</div></div></div></div>';
$counter++;
}
It doesn't work because you are executing the same query again and again.
Do that instead:
$res = mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."'");
while ( $rowresult = mysql_fetch_assoc($res) ) {
....
}
Also, I suggest you use MySQLi or PDO.
You are repeating your query on every iteration. Move the mysql_query() out of the loop to a variable.
$q = mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ");
while ($rowresult = mysql_fetch_assoc($q)) {
// ...
}
Put the query before the while loop. IE
$q=mysql_query("SELECT * FROM friends WHERE id='".$user["ID"]."' ");
while ( $rowresult = mysql_fetch_array($q)) {
...
But then of course use PDO... :)

How to just show few lines from a whole blog-post on a certain page?

I am making a blog in which I want to show just the summary of all posts on the home page rather than whole posts.
Below is the function that I use to get the posts on my home page.
function get_content($id = '')
{
if ($id != ""):
$id = mysql_real_escape_string($id);
$sql = "SELECT * from cms_content WHERE id = '$id'";
$return = '<p>Go back to Home page</p>';
echo $return;
else:
$sql = "select * from cms_content ORDER BY id DESC";
endif;
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) != 0):
while ($row = mysql_fetch_assoc($res)) {
echo '<h1>' . $row['title'] . '</h1>';
echo '<p>' . "by: " . $row['author'] . ", posted on: " . $row['date'] . '<p>';
echo '<p>' . $row['body'] . '</p><br />';
}
else:
echo '<p>We are really very sorry, this page does not exist!</p>';
endif;
}
And then simply I use this php code to get the posts:
<?php
if (isset($_GET['id'])) :
$obj->get_content($_GET['id']);
else :
$obj->get_content();
endif;
?>
So how to get the summary only using php function or mysql as I know that we can use mysql query to get limit the words?
function sumarize($your_string, $limit_lines){
$count = 0;
foreach(explode("\n", $your_string) as $line){
$count++;
echo $line."\n";
if ($count == $limit_lines) break;
}
}
sumarize($row['body'], 10);
will show first ten lines
1-lined:
echo '<p>'.implode("\n",array_slice(explode("\n",$row['body']),0,10)).'</p>';
instead of line with $row['body']

Categories