How can I make the page link appear along side the title? - php

I'm working on a search function of a flat HTML site and I've got it working but I want to add the page link to the results so people can navigate there but the page_link column won't appear next to it how can I make it appear?
if(isset($_GET['keywords'])) {
$keywords = $dbc->escape_string($_GET['keywords']);
$query = $dbc->query("
SELECT page_title
FROM contents
WHERE page_contents LIKE '%{$keywords}%'
OR page_title LIKE '%{$keywords}%'
");
}
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> result(s).
</div>
<?php
if($query->num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php
echo $r->page_title, $r->page_link;
?>
</div>
<?php
}

<?php
echo '<a href=' . $r->page_link . '>' . $r->page_title . '</a>;
?>

Related

php retrieve specific data onclick from database in a list format

<?php include ("../navbar.php"); ?>
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn,$query);
?>
<div class="container">
<div class="compound_report_card">
<h2>Compound Report</h2>
<div class="card-content">
<?php
//Check If Id Isset.
if(isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET[ "CHIHBT_ID" ] . "</p>";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_assoc($result )) {
?>
<ul>
<li>Canonical smiles : <?php echo $row['smile_notation']; ?> </li>
<li> Molecular weight : <?php echo $row['molecular_weight']; ?></li>
<li> Heavy atoms : <?php echo $row['number_of_heavy_atoms']; ?></li>
<li> Aeromatic heavy atoms : <?php echo $row['number_of_aeromatic_heavy_atoms']; ?></li>
<li> Rotatable bonds : <?php echo $row['number_of_rotatable_bonds']; ?></li>
<li> H bond acceptors : <?php echo $row['number_of_h_bond_acceptors']; ?></li>
<li> H bond donors: <?php echo $row['number_of_h_bond_donors']; ?></li>
<li> Tpsa A**2 : <?php echo $row['tpsa']; ?></li>
</ul>
<?php } ?>
</div>
</div>
</div>
<?php include ("../footer.php"); ?>
so this is my new code and i am displaying data on web from my database and . So what i am trying to do is i have a lot of id's and i want that when i click on any id the data of that id from
database should get displayed. So the current situation is that when i am clicking on one id whole data from that table is getting displayed and i want only one to get displayed. I want onlyspecific data from every id that i click. So the problem i think is in while loop so please see that also
The problem: Your code is getting all records and isn't querying against your selected ID.
What you need to do: Add selected ID in query to get specific result. Like shown below:
Update: From What I understand $_GET["CHIHBT_ID"] is your ID, right? Change this code in your script:
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn, $query);
?>
To
<?php include("../conn.php");
$query = "select * from semi_synthetic";
?>
Then, change the following code:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
$result = mysqli_query($conn, $query);
?>
With this:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"])) {
$query .= ' WHERE CHIHBT_ID = ' . $_GET["CHIHBT_ID"]; // add selected id in query
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
}
$result = mysqli_query($conn, $query); // run query
?>
and that's it.

this is my code. I want my search engine to turn the results into links to another page. idk how to

<?php
require_once 'db/connect.php';
if(isset($_GET['keywords'])) {
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("
SELECT title
FROM articles
WHERE body LIKE '%{$keywords}%'
OR title LIKE '%{$keywords}%'
");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results
</div>
<?php
if($query->num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->title; ?>
</div>
<?php
}
}
}
It appears that all you are querying for is the article title? if this is all you need for the url then just replace the
<?php echo $r->title; ?>
with
<?php echo $r->title; ?>
however if you need an id or something else then you will need to ensure your database query is returning that data for example
SELECT ID, Title FROM...
then use
<a href="http://example.com/<?php echo $r->id; ?>">...

PHP Show foreach() results from 2 SQLite-arrays, one as random

I have 2 SQLite- tables, one containing articles and one containing image URL's.
What I'd like to do is to do a foreach() to show the articles in order and show a set number of random images next to the article.
The images should be selected by checking similarity between the Article's and image's categories (column in the table) and then randomized. (The article catergory could be for example 'motorboats' and the img category 'boat').
How do I show the results from two different arrays?
The code for the articles is:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?>
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
To add the second array, I tried this, which didn't work, it only showed the results from the first array multiple times:
$stmt = $db->prepare('SELECT * FROM Article WHERE category = "article" ORDER BY pubdate DESC;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt2 = $db->prepare('SELECT * FROM Object;');
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="artikelLista">
<?php foreach($res as $article): ?>
<?php foreach($res2 as $object): ?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<?php echo $object['img'];?> <!-- For testing. The images should be filtered and a a few random images would be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
I guess it's not possible to use nested foreach() like this.
How will I manage this?
Also, If anyone knows on top of their head how to match the similarities between the arrays as described above, I'd be greatful. If not, I'll deal with this later.
You can fall a function inside the first foreach a pass category of the article to that function. Now in that functino collect all images in an array related
to the passed category and then return it by randomizing it. Code below ..
<?php foreach($res as $article): ?>
<?php
$img = get_img($article['category']);
?>
<div class="artikelContent">
<?php echo $article['title']; ?><br>
<?php echo $article['content'];?><br>
<img src="<?php echo $img; ?>" /> <!-- The return result will be shown here -->
<div class="floatRight clear"><?php echo "Artikel skriven " . $article['author'] . " " . $article['pubdate']; ?></div>
</div>
<?php endforeach; ?>
<?php
function get_img($category){
$stmt2 = $db->prepare('SELECT * FROM Object WHERE category = '.$category);
$stmt2->execute();
$res2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$rnd = array();
foreach($res2 as $object){
$rnd[] = $object['img'];
}
$n = rand(0,(count($rnd)-1));
return $rnd[$n];
}
?>

display text based on category in wordpress

how can i solve the following problem. Basically I want to show different sub headings based on the wordpress category being used.
this is the following code:
$categ = the_category(' ');
if($categ == 'News'){
$second_header = " secondry header displayed here";
}else{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Right now this is not working, instead of checking against the text 'news' is there any way to check against the category id?
thanks in advance.
You can use the following to store the current category id:
<?php $catID = the_category_ID($echo=false);?>
The echo false stops any echo on the page and stores the variable. You can then do the following:
<?php
$categ = the_category_ID($echo=false);
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Alternatively to this you could also use the following (as I believe the_category_ID is now deprecated http://codex.wordpress.org/Function_Reference/the_category_ID):
$categories = get_the_category();
$categ = $categories[0]->cat_ID;
if($categ == 1)
{
$second_header = " secondry header displayed here";
}
else
{
$second_header = "Error";
}
?>
<h2> <?php the_category(' '); ?></h2>
<p> <?php echo $second_header; ?></p>
Different Text on some Category Pages:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
if you need category id you will need to first get category through <?php get_the_category_by_ID( $cat_ID ); ?>
Because the_category_ID is deprecated, then you can do the same with some modification.
More here - http://codex.wordpress.org/Category_Templates

how to use html inside php while loop

I have user information coming from database on a profile page using a while loop. I want to be able to write stuff like this, $username's profile. I can't seem to figure it out. Here is my code.
<?php
//open database connection
include 'page-start.php';
include 'core/init.php';
?>
<?php
$myQuery = ("SELECT user_id, username, profile, city FROM `users` WHERE user_id = '" . mysql_real_escape_string($_GET['ID']) . "' ") or die(mysql_error());
//run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
} ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/studentsupport/defines.php'; ?>
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo '<div class="sixteen columns" id="user-profile">';
echo'<h2 class="username"> ' . $row['username'] . ' </h2> ';//bob's Profile
echo'<p>' . $row['city'] . '</p>'; //city: london
echo '<div class="eight columns" id="user-profile-img">';
echo'<img src="'. $row['profile'] . '"/>';
echo '</div>';
echo '</div>';
}
?>
edit: sorry I didn't explain it very well.
I want to be able to have some information come from the database and some information just as standard html for example:
<p><?php echo $username; ?>'s profile </p>
<p>city: <?php echo $city; ?> </p>
During each loop in the while, $row['username'] will have a username.
If you want to be able to get all the usernames later, add the following:
$users[] = $row['username'];
Now, Everywhere in your script $users[0] will have the 1st username, $users[1] will have the second, etc.
If you are beginner than i wil say You should use easy method now it seems okay
like You make Whole page of profile like you want
like
then you can Take your data in Variables in while Loop like that
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$profile = $row['profile'];
$name = $row['name'];//you can take as many variables as you want
$city = $row['city'];
}
then you can use these variables in html simply
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $name; ?> </h2>
</div>
start like that next step like above will get easy to u :)
This may do it.
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{ ?>
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $row['username']; ?></h2>
<p><?php echo $row['city']; ?></p>
<div class="eight columns" id="user-profile-img">
<img src="<?php echo $row['profile']; ?>"/>
</div>
</div>
<? } ?>

Categories