Reason for not able to identify pointer in localhost - php

I am going to retrieve some data from database and display it in php file.
It seems like it can connect to my localhost's database,because code like:
<?php foreach($goods as item ):?>
haven't occur any errors.
but when it comes to the codes like,
<?php echo $item->logo;?>
on my browser, it simply display
logo;?>
what's wrong with my codes or setting.
As the file is quite big and I think the system config problem(I have reinstalled wampserver), I just show a little bit
my code:
<?php echo sizeof($goods);?>
<td class="td_f"><IMG src="http://127.0.0.1:8020/UB_real//public/photos/frontimg/<?php echo $item->logo?>"> </td>
<?php endforeach; ?>

You have an error in your code:
Where you said:
<?php foreach($goods as item ):?>
Must be:
<?php foreach($goods as $item ); ?>
Note the missing $ before item, and the semicolon ; not the :
If you need to list all the returned values in $goods (for testing purposes), you can always do:
echo '<pre>';
print_r($goods);
echo '</pre>';

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

PHP does not output isset array from Mysql. Although through print_r the array is displayed

The Mysql command is set correctly, since the data is displayed correctly via print_r($ads). I pack the resulting array into $ads
Catch id
$id = htmlentities($_GET['id']);
Query DB.
SELECT
rent.id,
rent.run,
rent.year,
FROM rent
WHERE rent.id = '.$id.'
ORDER BY rent.time_upload DESC');
But through the isset function, they are not shown, no errors are displayed, nothing, just a blank page.
I output the data like this
<?php if (isset($ads)): ?>
<h3>
<?=$ads['id'];?>
<?=$ads['run'];?>
<?=$ads['year'];?>
</h3>
<?php endif; ?>
short_tags are included in PHP.
Please help solve the problem.
You're not going deep enough. $ads['run'] doesn't exist, only $ads['3625']['run'] does. Try:
<?php
foreach( $ads as $ad ) {
?>
<h3>
<?=$ad['id'];?>
<?=$ad['run'];?>
<?=$ad['year'];?>
</h3>
<?php
}

creating a space before a variable including htmlencode str_repeat

I have a variable given to me by my cms
I want to add one space before that but have tried many things lol and all dont work, below is what I have tried so far.
<?php echo str_repeat('', 1) htmlencode('$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('&nbsp$postcode_coveringsRecord['postcode']) ?>
<?php echo htmlencode('. $postcode_coveringsRecord['postcode'].) ?>
<?php echo '&nbsp''htmlencode('$postcode_coveringsRecord['postcode'])' ?>
<?php echo htmlencode('&nbsp''$postcode_coveringsRecord['postcode']) ?>
How can I minipulate
where as the variable gives me one blank space prior to the variable content.
cheers for any input
emma
These ones should work
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);
or
echo ' '.htmlencode($postcode_coveringsRecord['postcode']);

Extracting basic PHP echo from IF statement

The following code is for a Wordpress plugin, it displays points and tank of a user:
<?php
if(function_exists('cp_displayPoints') && $authordata->ID){
echo '<span class="cubepoints_buddypress">'; cp_displayPoints($authordata->ID); echo '</span>';
if(function_exists('cp_module_ranks_getRank')) echo ' <span class="cupepoints_buddypress_rank">'.cp_module_ranks_getRank($authordata->ID).'</span>';
}
?>
I am trying to extract these two echo functions from the If statement but only succeeded with one of them. I can echo the points like this:
<?php cp_displayPoints($authordata->ID); ?>
Works fine. Now I tried doing the same with the second echo:
<?php cp_module_ranks_getRank($authordata->ID); ?>
But it did not work. Obviously, there is some basic thing that I am missing here. Do you know what it is?
The first one likely prints directly to output, while the second returns its value. So, you need to echo() the second one, just as they're doing in your sample code:
<?php echo cp_module_ranks_getRank($authordata->ID); ?>

SimplePie RSS Not Getting pubDate

I am using SimplePie RSS to aggregate 4 feeds and they are being sorted by the date (descending) and in the code it is set to echo out the pubDate but it is not showing it. It just prints a blank element.
For sanaties sake (as the code file is tens of lines long I have it in a *.txt file on my server which can be found here: http://feeds.powercastmedia.net/feeds.php.txt
I am completely lost.
Cheers!,
Phill
Try placing other information in the echo calls to ensure that those lines are actually being called, and that the output is being displayed in the expected manor -
<title><? echo "Title: ".$item->get_title(); ?></title>
<link><? echo "Permalink: ".$item->get_permalink(); ?></link>
<pubDate><? echo "PubDate: ".$item->get_date(); ?></pubDate>
<description><? echo "Description: ".$item->get_description(); ?></description>
This kind of "debug output" can help with debugging all sorts of things. It should help you figure out exactly where the problem is originating from.
Also, I noticed that you have numerous unnecessary PHP opening and closing tags, where multiple lines could be consolidated into a single, cleaner code block (Ex:)
<?php if ($success): ?>
<? $itemlimit=0; ?>
<?php foreach($feed->get_items() as $item): ?>
<? if ($itemlimit==10) { break; } ?>
Could be cleaned up to be:
<?php
if($success)
{
$itemlimit = 0;
$items = $feed->get_items(); // This might also help, as PHP sometimes has issues when iterating through arrays returned directly from functions
foreach($items as $item)
{
if($itemlimit == 0) break;
...
In fact, most of the file could be within one pair of PHP tags.
Just a suggestion.

Categories