I am trying to load articles from 1 to 5 on the homepage of this website.
When loading the homepage, only results from ID 2 to 5 vs. 1 to 5 are getting displayed and I'm not sure why. It seems there probably is a problem with my while loop but I can't seem to figure it out.
I have added a link to a screenshot of the database to show that there is in fact an article with the ID 1 and I've also added a link to the website itself.
Database in question
Website in question
<div class="bodymainwrap">
<div class="contentwrap">
<?php
if (isset($_GET['id']) && !empty($_GET['id'])) {
$page = "between " . (($_GET['id']*5)-5) . " and " . ($_GET['id']*5);
} else {
$page = "between 1 and 5";
}
require_once '/db.connect';
$query = "SELECT * FROM Articles where id " . $page;
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result)
?>
<div class="sidebar" align="center">
<a href="/nothing/index.php?id=2" >Page 2</a>
<?php echo $query ?>
</div>
<?php
while ($row = mysqli_fetch_array($result)){
echo '<div class="entry">';
echo '<img src="/nothing/images/julie.jpg">';
echo'<H2>';
echo $row['Title'];
echo'</h2>';
echo '<p>';
echo $row['Article'];
echo '</p>';
echo '</div>';
}
?>
</div>
Your issue is here I believe, with the lone mysqli_fetch_array call after you get $result
$result = mysqli_query($link, $query);
if (!$result) {
echo "<br />" . $query;
die("<br/> Error: occured while trying to execute the query " . mysqli_error($link));
}
$row = mysqli_fetch_array($result) //delete this
Here you have already fetched the first row. Remove that, and just keep it within the loop. That's the underlying reason for how the while loop works. It keeps advancing while mysqli_fetch_array doesn't return null.
Related
Kinda new to PHP, trying to display only the file name for uploaded pdf's in my custom info box, not the complete url or ID. Been at it for two days searching the net and trying the code examples on acf homepage.
this is my current code:
<div class="post-content">
<?php the_content(); ?>
<!-- advanced custom fields plugin content -->
<?php
if( function_exists('get_field')) {
if (get_field('product_info_content') OR get_field('product_info_file') OR get_field('product_info_file_2') ) {
echo '<div class="info-box">';
echo '<h1>' . get_field('product_info_title') . '</h1>';
the_field('product_info_content');
echo '<ul>';
echo '<li class="product_info_list_item">';
the_field('product_info_file_1');
echo '</li>';
echo '<li class="product_info_list_item">';
the_field('product_info_file_2');
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?><!-- end advanced custom field -->
what I'm trying to active with little to no luck is echoing out the name of the file of product_info_file_1 and 2 as an link that opens in a new window.
Any help would be appreciated!
I believe, your field name is not uniform in if check and in display. You can try following code. This will display the link of file uploaded.
<?php
if( function_exists('get_field')) {
if (get_field('product_info_content') OR get_field('product_info_file_1')
OR get_field('product_info_file_2') ) {
echo '<div class="info-box">';
echo '<h1>' . get_field('product_info_title') . '</h1>';
the_field('product_info_content');
echo '<ul>';
echo '<li class="product_info_list_item">';
$file_1 = get_field('product_info_file_1');
if ( ! empty( $file_1 ) ) {
echo '' . esc_html( $file_1['title'] ) . '';
}
echo '</li>';
echo '<li class="product_info_list_item">';
$file_2 = get_field('product_info_file_2');
if ( ! empty( $file_2 ) ) {
echo '' . esc_html( $file_2['title'] ) . '';
}
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?><!-- end advanced custom field -->
Same problem but I used preg_match() to get the last word in the path so that way I can get the file name.
<?PHP
preg_match("/[^\/]+$/",get_field('filename'), $matches);
echo str_replace('-',' ',$matches[0]);
?>
i have this code in iclude for connect to database
i have in this file mysql connection function too but i decide change mysql to pdo
-include.php:
try{
$conn = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
return $conn;
}
catch(PDOException $e)
{
echo 'can not connect to database';
exit();
}
i have mysql query in index.php but i want change this code to pdo
<?php
$sql=mysql_query("SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18");
if(mysql_num_rows($sql)) {
$j=0;
while($result = mysql_fetch_object($sql)) {
$j++;
if($result->id == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $result->id; ?>&title=<?php echo title($result->col1);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $result->col1; ?></div>
</div>
</a>
<?php
}
}
}
?>
how can i change mysql query in this code to pdo?
Read some tutorial (there are plenty even here on SO)
Run some simple queries to make yourself familiar with PDO
Start with rewriting.
You should read some doc about PDO and do some tutorial. But to help you, here is a really simple sample to begin with PDO :
<?php
$connexion = new PDO("mysql:host=$PARAM_hote;dbname=$PARAM_db_name", $PARAM_user, $PARAM_pwd); // DB connexion
$results=$connection->query("SELECT member FROM member ORDER BY member ASC"); // retrieving all entries from member table
$results->setFetchMode(PDO::FETCH_OBJ); // retrieving results as objects
while( $lines= $results->fetch() ) // retrieving list of member
{
echo 'user: '.$lines->member.'<br />'; // displaying members
}
$results->closeCursor(); // close results cursor
?>
And here a more sophisticated one with params (prepared query) :
<?php
// connection opening ...
$prepared_query=$connecion->prepare("SELECT id FROM member WHERE member_id= :id"); // query prepare
$prepared_query->execute(array( 'id' => 1 ));
$lines=$prepared_query->fetch(PDO::FETCH_OBJ);
echo $lines->id.'<br />';
?>
It's an old sample of mine I hope will help you.
Something like this:
<?php
$sql = "SELECT id,col1 FROM tblname ORDER BY col4 DESC, col3 DESC, tzade DESC LIMIT 18 ";
$sth = $conn->query($sql);
$sth->setFetchMode(PDO::FETCH_ASSOC);
if ($count = $sth->rowCount() > 0){
$j=0;
while ($row = $sth->fetch()) {
$j++;
if($row['id'] == $site_id){
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadv">
<div><?php echo $j; ?></div>
<div><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
else{
?>
<a class="normal_link" href="l.php?lid=<?php echo $row['id'] ?>&title=<?php echo title($row['col1']);?>">
<div id="jadva">
<div id="jad"><?php echo $j; ?></div>
<div id="jad"><?php echo $row['col1'] ?></div>
</div>
</a>
<?php
}
}
}
?>
I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work
I have a form with only one input. When I click the button, an AJAX request is made and the data is sent to the response.php, which uses the pdo library to insert the data to db. Here it is okay, but in the page, there is a success function which loads the data into the form - when the ajax request completes, the result is blank...I need to refresh(F5) the page to see the data ..
Here is this code:
<?php
include_once("config.php");
if (!empty($_POST)) {
try{
$statement = $conn->prepare("INSERT INTO DIAGNOSTICO (id_paciente, id_doctor, hconsulta) VALUES (?, ?, ?)");
if ($statement->execute(array($_POST['id_paciente'], $_POST['id_doctor'], $_POST['hconsulta'])));
$dbSuccess = true;
} catch (Exception $e) {
$return['databaseException'] = $e->getMessage();
}
{
echo $conn->lastInsertId();
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
}
$dbh = null;
}
?>
It is working, but when in the page, the result returns from the ajax request and shows the id but not the data...I need the data result...can you help me?
best regards!
After using the lastinsertid you have to select data by using
$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
Read http://php.net/manual/en/pdostatement.fetch.php
View image: http://i.stack.imgur.com/yxE4u.jpg
Although this is in PHP, I'm trying to (with HTML) get two words (which use the same class) to push next to each other without calling the class seperately or using the two words in the same if/if else function. I'm sure it's just a simple HTML element.
<?php
if (something){
echo "<p class='message'>hello</p>";
}
if (somethingelse){
echo "<p class='message'>world</p>";
}
?>
As the comments aren't what I'm after, I'll show what I'm using:
<?php
if (!empty($message)){
echo "<p class='message'>" . $message . "</p>";
}
if (!empty($errors)){
echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
echo "</p>";
}
?>
One statement or both at the same time may prove true. Hopefully you can see why I can't wrap it all around an HTML tag.
i think you want to do this
<p class="messasge"> <?php echo $message; ?> </p>
RE-FIXED according to updated specifications :
<?php
if (!empty($message)){
echo "<p class='message'>" . $message;
}
if (!empty($errors)){
if (empty($message) echo "<p class='message'>";
foreach ($errors as $error){
echo " - " . $error . "<br />";
}
}
if ((!empty($message))||(!empty($errors))) echo "</p>";
?>
if JUST message is true it'll print <p class='message'>MESSAGE</p>
if JUST errors is true, it'll print <p class='message'>ERRORS</p>
if BOTH are true, it'll print <p class='message'>MESSAGEERRORS</p>
I think you probably want some variant on the following code:
<?php
$output = '';
if (something)
$output .= 'hello';
if (somethingelse)
$output .= 'world';
if ($output)
echo "<p class='message'>$output</p>";
?>
For your example, you could replace all of the echos in the first two conditions with appending to $message, then only output if there is something to output. There are numerous options but I think this is the most flexible / elegant.
In your case it can be simplified to,
<?php
$output = $message;
if (!empty($errors))
foreach ($errors as $error)
$output .= " - $error<br />";
if (!empty($output))
echo "<p class='message'>$output</p>";
?>
Maybe the dl list is useful
<?php
if (!empty($message)){
echo "<dl class='message'><dt>" . $message . "</dt>";
}
if (!empty($errors)){
foreach ($errors as $error){
echo "<dd> - " . $error . "</dd>";
}
echo "</dl>";
}
?>
<?php
$message = "";
if (something) $message = "hello";
if (somethingelse) $message += " world";
echo "<p class='message'>".$message."</p>";
?>