I'm wanting to fill a database with news article information such as PostID, PostTitle, PostDescription, PostContent and so on - I tried writing some code to constantly echo out the data in the SQL query but it only works for the first row that the query picks up
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
$row = mysql_fetch_array($result);
if (mysql_numrows($result) > 0) {
$row = mysql_fetch_array($result);
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
I then thought about doing a SELECT query to then count the number of ID's in the table and then set that as $ID and then give each variable a number and then +1 after each loop but realised that wouldn't work so I'm kinda stuck on how to loop out several rows automatically from my database.
Also, I know I shouldn't be using mysql_numrows and whatnot in PHP and I should move to mysqli or PDO but this is for something where I've been asked to use mysql specifically.
You're only fetching one row. Try looping through your results.
$SQL = "SELECT * FROM posts"; // get all info from posts
$result = mysql_query($SQL);
while( $row = mysql_fetch_array($result);)
{
$Title = $row["PostTitle"];
$Description = $row["PostDescription"];
$Date = $row["PostDate"];
$AuthorID = $row["AuthorID"];
echo "<div id='preview'>";
echo "<div class='group selection'>";
echo "<div class='col span_1_of_3'>";
echo "<div id='previewmedia'>";
echo "</div>";
echo "</div>";
echo "<div class='col span_2_of_3'>";
echo "<h1>".$Title."</h1>";
echo "<p class='preview'>".$Description."</p>";
echo "<a href='bloglayout.php' class='orange readmore'>Read More...</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
Related
I'm Building a Simple PHP Page for MCQS (Multiple Choice Questions). I'm Using MySQL Database to retrieve data. I want to Display Options / Alternatives ( i.e: field_correct_answer_value, field_wrong_answer_1_value, field_wrong_answer_2_value, field_wrong_answer_3_value, field_wrong_answer_4_value) in Random Order with stylesheet so i can identify which one is correct.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
echo "<div id=a1>". $row["field_correct_answer_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_1_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_2_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_3_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_4_value"]."</div>";
}
Is it Possible?
($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//FOR RANDOMIZATION OPTION COLLECTING DATA IN ARRAY AND SHUFFLING THEM
$corr = "<div id=a1>" . $row["field_correct_answer_value"] . "</div>";
$wor1 = "<div id=a2>" . $row["field_wrong_answer_1_value"] . "</div>";
$wor2 = "<div id=a2>" . $row["field_wrong_answer_2_value"] . "</div>";
$wor3 = "<div id=a2>" . $row["field_wrong_answer_3_value"] . "</div>";
$wor4 = "<div id=a2>" . $row["field_wrong_answer_4_value"] . "</div>";
$opt = array($corr, $wor1, $wor2, $wor3, $wor4,);
$opt_ran = shuffle($opt);
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
//FOR DISPLAYING RANDOM OPTIONS
foreach ($opt as $number) {
echo $number;
}
}
I have my array looping and building this columns :
page link,
i want to group the users if they repeat, i have this number for each on row (pid) i want that all the PID with the same number to group.
maybe with some kida of if statement i can add classes to duplicates and singles in array! please help
// $strSQL = "SELECT *, SUM(totalparcial) as Soma FROM bruno_wallet GROUP BY nome ";
$strSQL = "SELECT * from bruno_wallet GROUP BY id ORDER BY nome ASC ";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row1) using mysql_fetch_array
$sum = 0;
$data = array();
while($row1 = mysql_fetch_array($rs)) {
$events = array_unique($row1);
$data[] = $events;
}
//now loop over data instead of mysql_fetch_array
foreach ($data as $events) {
$currentPID = $events[12];
echo "<div id='linha_$events[0]' class='cor promotora_$events[12] cf'>";
echo "<div class='tb1'>{$events[0]}</div>"; //id
echo "<div class='tb1'>{$events[7]}</div>"; //foto
echo "<div class='tb1'>{$events[3]}</div>"; //data
echo "<div class='tb1'>{$events[2]}</div>"; //nome
echo "<div class='tb1'>{$events[12]}</div>";
echo "<div class='tb1'>{$events[4]}</div>"; //evento
echo "<div class='tb1'>{$events[5]}</div>"; //horario
echo "<div class='tb1'>{$events[6]}</div>"; //obs
echo "<div class='tb1'>{$events[10]}h</div>"; //horas
echo "<div class='tb1'>{$events[9]}€</div>"; //valor hora
echo "<div class='tb1'>{$events[1]}</div>"; //Props
echo "<div class='tb1'>{$events[8]}</div>"; //tparcial
echo "</div>";
?>
this worked for me :
if (in_array($currentPID, $checkedPIDs)) {
// build multiples
} else {
// build unikcs
array_push($checkedPIDs, $currentPID); ?>
I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
I have just created a pagination function for a travel company. There are two things I want to change that I can't figure out.
Each trip has a date. What i want to be able to do is stop the trip from displaying in the list one the day of the trip and all subsequent days. I have a rough idea, but that doesn't seem to work.
if (date($row['date']) getdate()) {
// echo trip info here
}
-2. After each of the trips in the list there is a seperator (is that the right word?) to separate each trip's info. On the last trip if the page I don't want that seperator. I have no clue how to do this :/
Thanks in advance for any help & advice.
<?php
if (isset($_GET["p"])) { $page = $_GET["p"]; } else { $page=1; };
$start_from = ($page-1) * 4;
$sql = "SELECT * FROM destinations ORDER BY date ASC LIMIT $start_from, 4";
$result = mysqli_query ($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<div class='row'>";
echo "<div class='col-md-4 col-sm-4'>";
echo "<img src='" . $row ['img'] . "' alt='' class='img-responsive'>";
echo "</div>";
echo "<div class='col-md-8 col-sm-8'>";
echo "<h2><a href='" . $row ['url'] . "'>" . $row ['name'] . "</a></h2>";
echo "<ul class='blog-info'>";
echo "<li><i class='icon-calendar'></i> " . date("d/m/y", strtotime($row['date'])) . "</li>";
echo "</ul>";
echo $row ['description'];
echo "<a class='btn theme-btn' href='" . $row ['url'] . "'>View Details <i class='icon-angle-right'></i></a>";
echo "</div>";
echo "</div>";
echo "<hr class='blog-post-sep'>";
}
$sql = "SELECT COUNT(trip_id) FROM destinations";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);
echo "<div class='text-center'> <ul class='pagination pagination-centered'>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='routea.php?p=".$i."'>".$i."</a></li>";
};
echo "</ul></div>";
mysqli_close($con);
?>
Change your SQL to filter out the unwanted trips:
SELECT * FROM destinations
WHERE date < CURRENT_DATE()
ORDER BY date ASC
LIMIT $start_from, 4
You can solve it with CSS:
hr.blog-post-sep:last-of-type {
display: none;
}
this file is included in my index file, i want not more than 5 posts on page and under posts 1 2 3 4... and etc. (links to the next pages) and that it look like this index.php?page=2
Sorry for my bad grammar.
<?php
if(isset($_GET['post_edit'])) {
$p_id = $_GET['post_edit'];
$p_query = mysql_query("SELECT title, post FROM posts WHERE id='$p_id'");
$p_array = mysql_fetch_array($p_query);
$title = $p_array['title'];
$post = $p_array['post'];
}
?>
<?php
if(isset($_POST['edit'])){
$title_edit = $_POST['titleedit'];
$post_edit = $_POST['postedit'];
if(empty($title) or empty($post)){
echo "<p>Fields empty!</p>";
} else {
mysql_query("UPDATE posts SET title='$title_edit', post='$post_edit' WHERE id='$p_id'");
echo "Edit succesful!</br>";
header('location: index.php');
}
}
?>
<?php
if(isset($_GET['post_edit']) && !empty($_GET['post_edit'])){
include 'edit_post.php';
} else {
?>
<?php
$query = mysql_query("SELECT * FROM posts ORDER BY date DESC");
while($row = mysql_fetch_array($query)){
echo "<div class='poststitle'>";
echo "<div style='font-weight: bold;'>";
echo $row['title'];
echo "</div>";
echo "</div>";
echo "<div class='posts'>";
echo $row['post'];
echo "</br>";
echo "<hr>";
$user_name = mysql_query("SELECT username FROM users WHERE id = '".$row['user']."' ");
$user_name_array = mysql_fetch_array($user_name);
$post_id = $row['id'];
echo "Posted by: <b>";
echo $user_name_array['username'];
echo "</b> | ";
echo "Views: <b>";
echo $row['views'];
echo "</b> | ";
echo "Posted on: ";
echo "<b>";
echo $row['date'];
echo "</b><hr>";
echo '</div>';
if (loggedin()){
if($user_level == 1){
echo "<div class='postoptions'>";
echo "<a href='index.php?post_edit=$post_id'><img src='img/optionicons/edit.png' width='15' height='15' alt='edit' /></a>";
echo "<a href='del_post.php?del=$post_id'><img src='img/optionicons/cancel.png' width='15' height='15' alt='Delete' /></a>";
echo "</div>";
} else {
echo "";
}
}
}
}
?>
SELECT * FROM table_name LIMIT N, M
Where N - number of limited rows and M(offset) = N * (PAGE -1)
You should use the LIMIT clause in your MySQL query, see the docs
in order to calculate the numbe rof pages, you also need the total number of rows, this can be found using SQL_CALC_FOUND_ROWS, see the docs or this question