is there is any way to define a variable under the while loop and print it above the while loop
something like this
print $catName
while ($b = mysqli_fetch_assoc($results)) {
$catName = $b['cat'];
}
EDIT
$getBooks = $db->prepare('SELECT
id, cat, instituteId, title, cover, authorName FROM books_library WHERE instituteId=? AND cat=?');
$getList = array();
$getBooks->bind_param('ii', $inId, $cid);
if ($getBooks->execute()) {
$libResults = $getBooks->get_result();
$getList[] = $libResults;
?>
HTML CODE COME HERE
<h3 class="marginBottom8px margin-top_30px text-bold text-danger"><?php echo catName ?></h3>
AND THEN THE WHILE LOOP
while ($book = mysqli_fetch_assoc($libResults)) {
$bokId = $book['id'];
$bookTitle = $bokId['title'];
$catName = $bokId['cat'];
$catName = $bokId['cat'];
now under the while loop I got the $catName How can I echo it above in the <h3></h3>
After the while block, put your h3 html in a variable and print it.
$myHtml = '<h3 class="marginBottom8px margin-top_30px text-bold text-danger">'
. $catName . '</h3>';
print $myHtml;
Don't mix html and php like that.
I think this is what you're after based on the edited question
while ($book = mysqli_fetch_assoc($libResults)) {
echo '<h3>' . $book['cat'] . '</h3>';
}
Related
So I am creating a dynamic menu. Where I have stored the main categories of the menu in a separate table and subcategories in another table.
They are stored in this way
Categories Table
id cat_name
1 HOME,
2 PRODUCTS,
3 SOLUTIONS,
4 NEWS & GALLERY,
5 DOWNLOADS,
6 CONTACT
Right Now I am running a query
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($query)) {
$row['cat_name'];
$cat=explode(",",$row['cat_name']);
}
Then wherever needed I am printing the values like this
<?php echo $cat[0]; .. echo $cat[1]; //and so on ?>
It looks like this right now. And it is supposed to look likethis
But the problem with this solution is that I have to define the index of the array to print it.
I am developing the admin panel for this one and I want that if the admin adds a new menu item then it should automatically fetch it and display it.
But with this solution it is not possible.
I am thinking of iterating the values with a for loop but cannot get it right.
Use foreach ($query as $rows)
or
Use for ($i=0; $i < count($query); $i++)
Probably You can try this also
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($query))
{
$row['cat_name'];
$cat=explode(",",$row['cat_name']);
foreach($cat as $catss)
{
$cat = trim($catss);
$categories .= "<category>" . $cat . "</category>\n";
}
}
You can try this way may be you wish like this for dynamic menu:
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$menu = '';
$menu .= '<ul>';
while($row = mysqli_fetch_assoc($query))
{
$menu .= '<li>' . $row['cat_name'] . '</li>';
}
$menu .= '</ul>';
echo $menu;
When you are fetching data from table then you can directly print that value using field name. if you want to further use that so, you have to add in one array like:
$cat[] = $row['cat_name'];
then you can use this value in that manner using for or while:
foreach ($cat as $value) {
echo $value;
}
echo '<ul>';
foreach ($cat as $val) {
echo "<li>{$val}</li>";
}
echo '</ul>';
Instead of the index. Also this should be faster and readable than for loop.
You can save it in array then later access the values.
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$cat = array();
while($row = mysqli_fetch_assoc($query)) {
$cat[]=$row['cat_name']
}
Now:
foreach ($cat as $category) {
$cate_gory = explode("," , $category);
foreach ($cate_gory as $categories) {
echo $categories . "<br>";
}
}
Foreach Loop
foreach($cats as $cat)
{
echo $cat;
}
Basic For Loop
for ($i=0; $i<count($cats); $i++)
{
echo $cats[$i];
}
Simple Use Case
<?php
$cats = 'Stack overflow PHP questions';
$cats = explode(' ',$cats);
$html = '<ul>';
foreach($cats as $cat)
{
$html .= '<li>' . $cat . '</li>';
}
$html .= '</ul>';
echo $html;
?>
Your Code
In this case the while loop you are using will iterate over all the values returned from the sql statement, so there is no need to add more complexity. I would remove the commas from the field names and echo out the html this way.
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$html = '<ul>';
while($row = mysqli_fetch_assoc($query)) {
$html .= '<li>' . $row['cat_name'] . '</li>';
}
$html .= '</ul>';
echo $html;
?>
below is my PHP code, I just can't figure out how to make the "INFO" text appear as a clickable url. I've seen the echo method but not one that works for the below. This method needs to create a link using the data and also set the link as the text used inside the table.
<?php
if(isset($data) && !empty($data)) {
foreach ($data as $row) {
#$count += 1;
$keyword = htmlspecialchars($row['Keyword']);
$suburb = htmlspecialchars($row['Suburb']);
$postcode = htmlspecialchars($row['Postcode']);
$status = htmlspecialchars($row['Status']);
$info = htmlspecialchars($row['Info']);
#$table_row .= "<tr><td>$count</td><td>$keyword</td><td>$suburb</td><td>$postcode</td><td>$status</td><td>$info</td></tr>";
}
echo #$table_row;
}
?>
It's the 'Info' column that i want to pull in as a url.
Cheers,
Curtis
http://www.homegiraffe.com.au
$info = '' . htmlspecialchars($row['Info']) . '';
This is the answer. Thanks to Chris85 for the help!
you can use this
<?php
if(isset($data) && !empty($data)) {
foreach ($data as $row) {
#$count += 1;
$keyword = htmlspecialchars($row['Keyword']);
$suburb = htmlspecialchars($row['Suburb']);
$postcode = htmlspecialchars($row['Postcode']);
$status = htmlspecialchars($row['Status']);
$info = htmlspecialchars($row['Info']);
?>
<tr><td><?php echo $count;?></td><td><?php echo $keyword;?></td><td><?php echo $suburb;?></td><td><?php echo $postcode?></td><td><?php echo $status;?></td><td><?php echo $info;?></td></tr>;
<?php
}
}
?>
and it work very good :)
Currently, I am able to scrape the content from my desired website without any problems, but if you view my demo, you can see that in my array it's only displaying The Source no matter what I change around, it's not fixing..
$page = (isset($_GET['p'])&&$_GET['p']!=0) ? (int) $_GET['p'] : '';
$html = file_get_html('http://screenrant.com/movie-news/'.$page);
foreach($html->find('#site-top ul h2 a') as $element)
{
print '<br><br>';
echo $url = ''.$element->href;
$html2 = file_get_html($url);
print '<br><br>';
$image = $html2->find('meta[property=og:image]',0);
print $news['image'] = $image->content;
print '<br><br>';
// Ending The Featured Image
$title = $html2->find(' header > h1',0);
print $news['title'] = $title->plaintext;
print '<br>';
// Ending the titles
print '<br>';
$articles = $html2->find('div.top-content > article > p');
foreach ($articles as $article) {
echo "$article->plaintext<p>";
}
$news['content'] = $article->plaintext;
print '<br><br>';
#post> div:nth-child(2) > header > p > time
$date = $html2->find('header > p > time',0);
$news['date'] = $date->plaintext;
$dexp = explode(', ',$date);
print $date = $dexp[0].', '.$dexp[1];
print '<br><br>';
$genre = "news";
print '<br>';
mysqli_query($DB,"INSERT INTO `wp_scraped_news` SET
`hash` = '".$news['title']."',
`title` = '".$news['title']."',
`image` = '".$news['image']."',
`content` = '".$news['content']."'");
print '<pre>';print_r($news);print '</pre>';
}
Currently using simple_html_dom.php to scrape.
If you take a look at this piece of code:
$articles = $html2->find('div.top-content > article > p');
foreach ($articles as $article) {
echo "$article->plaintext<p>";
//This is printing the article content line by line
}
$news['content'] = $article->plaintext;
//This is grabbing the last line of the article content AKA the source
//The last <p> as it's not in the foreach.
Effectively, you need to be doing this:
$articles = $html2->find('div.top-content > article > p');
foreach ($articles as $article) {
echo "$article->plaintext<p>";
$news['content'] = $news['content'] . $article->plaintext . "<p>";
}
I am trying to extract download links from a site. However i only get the last item in my array.
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
foreach ($dllinks as $value)
{
echo $value . '<br>';
}
?>
When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.
EDIT:
Sorry it was supposed to be like this
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
?>
I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('.tlistrow') as $row){
$link_nodes = $row->find('td.tlistdownload a');
$name_nodes = $row->find('td.tlistname a');
if (count($link_nodes) > 0 && count($name_nodes) > 0) {
$link = $link_nodes[0]->href;
$name = htmlentities($name_nodes[0]->innertext);
echo "<a href='{$link}'>{$name}</a>\n";
}
}
I´ve got the a php that returns a JSON string:
$recipes = json_encode($arr);
That is my php-code how I output the recipe-title:
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
$i = 1;
var data = include('php/getAllRecipes.php')Data.Recipes;
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo "$i.)   ";
echo $recipe['TITLE'];
$i = $i + 1;
echo "<br>";
}
?>
Now, I need to add a href to each title. The href should contain a link to recipe_search.php and I have to give it the id of each recipe.
How can I add this href?
<?php
include('php/getAllRecipes.php');
$jsonstring = $recipes;
$recip = json_decode($recipes, true);
?>
<ol>
<?php
foreach ($recip['Data']['Recipes'] as $key => $recipe) {
echo '<li>
<a href="/recipe_search.php?id=' . $recipe['ID'] . '">
' . $recipe['TITLE'] . '
</a>
</li>';
}
?>
</ol>
Use an ordered list (<ol>) instead of trying to create one yourself using a counter.
var data = include('php/getAllRecipes.php')Data.Recipes; is not valid PHP.
I assume that the id of the recipe is in $recipe['ID'].
Here you are...
foreach ($recip['Data']['Recipes'] as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe['TITLE']);
}
Thats worked for me, just to test the above:
<?php
$i = 1;
foreach (array_fill(0, 40, 'recipe') as $key => $recipe)
{
// I guess $key is ID of your recipe...
echo sprintf('%d.) %s<br />', $i++, 'recipe_search.php?id=' . $key, $recipe);
}
?>