How to change loadObjectList() item positions? - php

I tried to change the positions of the loadObjectList(). but it is not working. can anyone help me to solve this problem?
This is the code i used.
$catID = 8;
//echo $catID;
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
$db = JFactory::getDBO();
$db->setQuery("SELECT title,alias FROM #__content WHERE catid = ".$catID);
$articles = $db->loadObjectList(); ?>
<div>
<?php foreach($articles as $article){
$title = $article->title;
if($title == $page_title){?>
<h4><?php echo $article->title; ?></h4>
<?php
}else{ ?>
<h4><?php echo $article->title; ?></h4>
<?php }
}
?>
</div>
what i want to do is, i want to display the page title as the first element of the list. Thats means if condition's item should display first.
can anyone helpe me?

A quick dry solution is to add 2 foreach loops and first display the value if condition match and after the rest values.
<?php
foreach($articles as $article){
$title = $article->title;
if($title == $page_title){
echo '<h4>'. $article->title .'</h4>';
}
}
foreach($articles as $rest_articles){
$title = $rest_articles->title;
if($title != $page_title){
echo '<h4>'. $rest_articles->title .'</h4>';
}
}
?>
A better solution is to store values in a new array and display them afterwards.
Hope this helps

Just had a double take here. Multiple foreach loops can be used, however are not required. Try the following:
<?php
foreach($articles as $article) {
$title = $article->title;
$alias = $article->alias;
if($title == $page_title) {
echo '<h4>'. $title .'</h4>';
}
else {
echo '<h4>'. $title .'</h4>';
}
}
?>

Related

How to append count to variables in php

I am just starting to learn PHP and to compound the issue I am trying to modify existing code to match what I am attempting to do. I have multiple variables I am trying to define all while partially using a counter. My problem is appending the count to my individually defined variables. Also, I'm sure there is a cleaner way to do this (like a loop or nested array), I just can't see how.
<?php
$pageId0 = opt('first_tab_page');
$post0 = get_post($pageId0);
$content0 = apply_filters('the_content', $post0->post_content);
$icon0 = wp_get_attachment_image_src( get_post_thumbnail_id( $post0->ID ), 'full' );
$pageId1 = opt('second_tab_page');
$post1 = get_post($pageId1);
$content1 = apply_filters('the_content', $post1->post_content);
$icon1 = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'full' );
$pageId2 = opt('third_tab_page');
$post2 = get_post($pageId2);
$content2 = apply_filters('the_content', $post2->post_content);
$icon2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'full' );
?>
<div>
<?php echo $icon0[0]; ?>
</div>
<div>
<?php echo $icon1[0]; ?>
</div>
<div>
<?php echo $icon2[0]; ?>
</div>
<?php
$tab_position = opt('tab_position');
if ($tab_position == '' || count($tab_position) != 3) {
$tab_position = array(0, 1, 2);
}
for($i=0; $i < count($tab_position); $i++)
{
$selected = '';
if (opt('default_selected_tab') == $tab_position[$i]){
$selected = 'class="selected"';
}
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $content//should be 0 to start; ?>
</a>
<?php } ?>
Here is a short example on using arrays. comment in code
// Storing tab names to an array, just add more tabs if required
// and details for all those will be loaded in the following arrays
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
//declare arrays to store (not required, but better practice)
$pageids = array();
$posts = array();
$content = array();
$icons = array();
//Iterate through the tabs array
foreach ($tabs as $tab){
//Store page id and post in a variable, we require it
$pageid = opt($tab);
$post = get_post($pageid);
// Store pageid in pageids. note the [] this means it will store pageid at next index
//also store other items
$pageids[] = $pageid;
$posts[] = $post;
$contents[] = apply_filters('the_content', $post->post_content);
$icons[] = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
}
now $icons[0] will contain first icon $icons[1] the second one and so on. Same thing applies to other variables.
Note: this code is just typed in here and not checked. Please fix if there is any syntax errors. Also note this is one way and there are more ways.
But I would suggest keeping data of each page together.
Edit: Here is how you can keep things together (only relevant parts shown)
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
$pages = array();
foreach ($tabs as $tab){
$pageid = opt($tab);
$post = get_post($pageid);
$content = apply_filters('the_content', $post->post_content);
$icon = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
//Store everything to $pages array
$pages[] = array('pageid' => $pageid, 'post' => $post, 'content' => $content, 'icon', $icon);
}
foreach ($pages as $page){
?>
<div>
<?php echo $page['icon'][0]; ?>
</div>
<?php } ?>
foreach ($pages as $page){
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $page['content']; //echo the content of page ?>
</a>
<?php } ?>
Instead of foreach to display you can use index also like
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[0]['content']; //echo the content of the first page ?>
</a>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[1]['content']; //echo the content of the second page ?>
</a>
I guess there is no other way then (nested) arrays. Also I'm thinking that arrays are a acceptable solution.
foreach ($tabs as $tab) {
$tab['content'] = 'somecontent';
$tab['counter'] = 'counter';
}
or
foreach ($tabs as $tab) {
$tab = [
"content" => "content",
"counter" => "counter"];
}
I'm not sure what you are trying to do as your question is not very clear to me. But i think you are trying to echo $content in your last line of code. If yes, just put your all your variables $content1, $content2 and $content3 in an array like below:
$contents = array($content1, $content2, $content3);
Then use foreach loop and traverse this array like this.
$count = 0;
foreach($contents as $content) {
echo $content.$count;
$count++;
}
Try and let me know if it works for you.

print variable above of the while loop (scope) php

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>';
}

Getting last record value only

I am getting only the last element value, like in my case i am trying to get value of $dwnld_name
but don't know where i am missing, so only getting download name for the last record
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
echo $link = $dwnld_row['link'];
echo $dwnld_name = $dwnld_row['name'];
$cat_id = $dwnld_row['category'];
}
?>
<?php
$sql = "SELECT * FROM wp_dm_category";
$myquery = mysql_query($sql);
echo '<ul>';
while($row = mysql_fetch_array($myquery)){
$x = $row['name'];
echo $x_id = $row['id'];
?>
<li><a href="#"><?php echo $x; ?>
<?php if($x_id == $cat_id) { ?>
<ul>
<li><?php echo $dwnld_name; ?></li>
</ul>
<?php } ?>
</a></li>
<?php echo "<br/>";
}
echo '</ul>';
?>
It is because, you are taking the values from loop in strings.
Everytime loop runs, it overwrites the values by recent values.
You can create an array and append values to that and loop through that array using foreach
Corrected Code:
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
$downloads = array();
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
$downloads['link'] = $dwnld_row['link'];
$downloads['dwnld_name'] = $dwnld_row['name'];
$downloads['cat_id'] = $dwnld_row['category'];
}
?>
Now, loop through $downloads array.
Note: Do not use mysql_ functions are they are deprecated and will be removed in future versions of PHP.
You need to echo $dwnld_name in the loop, or save names into array.
With this solution you overwrite $dwnld_name variable in each loop and as a result after the loop you have the last record.

foreach in foreach only echos last item?

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";
}
}

php foreach : each link in separate variable

I get some links from feed with this code on simplepie :
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links = $item->get_permalink();
echo $links;
endforeach; endif;
thats result me:
http://link1....
http://link2....
http://link3....
i want to put each link in separate variable like :
$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';
thanks , mori
$i = 0;
foreach($feed->get_items(0,3) as $item)) {
${'link' . ++$i} = $item->get_permalink();
}
Maybe what you want is array variable?
try this:
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links[] = $item->get_permalink();
endforeach; endif;
if ($check) :
$i=0;
foreach ($feed->get_items(0,3) as $item):
$links.$i = $item->get_permalink();
echo $links.$i;
$i++;
endforeach; endif;
I think it may works...

Categories