I am having trouble with my code. I want to display posts on one page grouped by category but I also need the titles of the posts to be links to the actual articles, everything I’ve tried so far has broken. Can anyone help? I can echo the titles just not as links!
You should take the excellent advice from #Marten and #CharlesAddis in the comments, and if you don't want to use an ORM, you should at least use PDO.
However, to give an answer for answer's sake:
while ($row = mysqli_fetch_array($result)){
echo "<h2>".$row['category']."</h2>"."<br />";
$titles = explode(", ",$row['titles']);
foreach ($title as $t) {
echo '' . $t . '<br>';
}
echo "<hr />";
}
I'm not sure what the relation between post titles and post links are, if you can add that I can update the answer.
Related
I am using woody snippets to insert php code to make an API call to return information in a table. How do I get the table to display using the default css from my wordpress theme?
I am new to PHP, but not to wordpress. And I know just enough CSS to get myself in trouble. So, I need ELI5 instructions.
My programmer provided the basic PHP code to call the API and return the data in a basic table. But, it looks horrid and there's no spacing or styling.
$tableStart = "<table>";
$tableEnd = "</table>";
$trStart = "<tr>";
$trEnd = "</tr>";
$tdStart = "<td >";
$tdEnd = "</td>";
$fname = 'alabama_energy_data.json';
$data = json_decode(file_get_contents($fname));
echo $tableStart;
foreach($data as $bill_data)
{
echo $trStart;
echo $tdStart . $bill_data->bill_id . $tdEnd . $tdStart . $bill_data->title . $tdEnd;
echo $trEnd;
}
echo $tableEnd;
This returns a basic table with the data I need, but it's not styled. How do I get it to use the default CSS from our wordpress site that it's displaying in so that the table renders in a format that looks decent?
If your table isn't styled like it should be, then you're probably missing and id or class attribute at your table. You could check on what current attributes your "wordpress" tables have, and just add them to your table.
Example:
<table class="wp-class-table">
# OR
<table id="wp-id-table">
And one other thing, I personally don't really like the way you "echo" your data. If you want, you can read this answer. It shows a nicer way to do it in my opinion.
I was hoping somebody may be able to help me. I'm having trouble with if statements with the Advanced Custom Fields plugin for Wordpress. I've got three options the user can choose from, all three can be chosen, but they can also choose just one if they wish.
The issue I'm having is the code I've written displays all of the HTML tags, even the empty ones. This is causing styling issues. I want to be able to just show HTML that has been populated. I've tried the solutions on the ACF forums but to no avail.
Link: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Here's the quick (newbie!) code I've got at the minute:
<?php the_sub_field('link'); ?>
<?php the_sub_field('doc'); ?>
<p><?php the_sub_field('cap'); ?></p>
I looked on the ACF forum and tried this, but it broke the theme:
<?php if(the_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(the_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(the_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
I'm looking for some help to make this work. I don't think I'm too far away from the right answer, however I'm a bit of a rookie with anything beyond standard front-end stuff, any thoughts would be very much appreciated.
Thanks!
Try to use get_sub_field();
<?php if(get_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(get_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(get_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
When looping through one of these fields, this function returns a sub field from the current row.
Like Dk-Macadamia said, try to use get_sub_field() in loops instead of the_sub_field()
the difference is get_sub_field() return the value as a string, and the_sub_field() print the data,
Also get_sub_field() only work under a repeater/ fluid field type otherwise wont work,
if its not a sub field of repeater/fluid fields just try get_field()
So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.
$_SESSION are coming from another page.. when i run a search on my search engine the # of results shows up but stays on the page forever and ever.. have a look (gawd i need 10 rep points to post images)
well basically 0 results is always shown and below it are the results of the search which is always there as well.
0 results for "pepsi"
2 results for "flash"
flash - website tutorial
flash design - come learn from the best
this is basically what the search page looks like only the search result links and descriptions will disappear if the search result produces no results. i would like to have everything disappear on every new search.. even when i refresh teh page or come back to it the results from the previous search are there.
<div id="searchPageDiv">
<?php
if(isset($_SESSION['error'])){
echo $_SESSION['error'];
} else
print_r($_SESSION['search_output']);
{
echo ($_SESSION['count']);
foreach($_SESSION['search_output'] as $value){
$value['links'];
$value['title'];
$value['page_body'];
$title = $value['title'];
$link = $value['links'];
$body = $value['page_body'];
$search_output .= "<a href='".$link."'>".$title."</a> - $body<br>";}
}
?>
<?php echo $search_output; ?>
</div>
This will be helpful:
echo ($_SESSION['count']);
unset($_SESSION['count']);
Studying is best approach. Look for examples given in www.php.net/unset
I am working on building a news section for one of my sites. I am storing the information for each news article in a database table news, the main text of the article is in column copy whose type is set to longtext.
I use the following line of code to display the news article as paragraphs:
<p class="news_copy"><?php echo preg_replace('/[\r\n]+/', '</p><p class="news_copy">', $news['copy']); ?></p>
and it works absolutely perfect.
The reason for this post is because I would like to be able to display more than one photo for each news article. I am setting a lead image in the database, but if there were, say, 4 other photos in one story and 6 in another (in addition to the lead image) is there any way for me to display, say, 1 or more photo(s) per paragraph?
I'll be happy to elaborate in case this is too confusing.
Many thanks!
<?php echo insert_par_and_photos($news['copy'], $news['photos']); ?>
function insert_par_and_photos($copy, $photos) {
$pars = preg_split('/[\r\n]+/', $copy);
$result = '';
foreach ($pars as $i => $par) {
$result .= '<p class="news_copy">';
$result .= $par;
if ($i < count($photos)) {
$result .= '<img src="'+$photos[$i]+'"/>';
}
$result .= '</p>';
}
return $result;
}