Display second value from mysql by foreach loop - php

Sorry, If my question is not good.
I am using this code to display values from mysql database.
<?php foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>
It is displaying result like this
-> http://pkbazaar.com/realoffers/wp-content/Cimy_User_Extra_Fields/riaz/avatar/aget-3.png
-> Johar Town
-> 1234567
-> 54000
-> Australia
-> WA
-> Lahore
But I don't want to display first value like "http://localhost/realoffers/wp-content/Cimy_User_Extra_Fields/riaz/avatar/aget-3.png"
What I should do to start displaying value from second value like "Johar Town".

If you don't need the first value you can shift the element off the beggining of array.
<?php $old_value = array_shift($values); ?>
<?php foreach ($values as $value) : ?>
<span>
<?php echo cimy_uef_sanitize_content($value['VALUE']); ?>
</span>
<?php endforeach; ?>

<?php $count = 0;
foreach ($values as $value) {
if($count == 0 ) {
$count++;
} else { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php }
} ?>

Try this:
<?php
foreach ($values as $k=>$value) {
if ($k==1){
?><span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span><?php
}
}
?>

If the number of records is the same in each array and a fixed value, it would be fairly simple to use the following:
<?php
foreach ($values as $value)
{
?>
<span><?php echo cimy_uef_sanitize_content($value['FIELD2']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD3']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD4']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD5']); ?></span>
<span><?php echo cimy_uef_sanitize_content($value['FIELD6']); ?></span>
<?php
}
?>

It's probably better to change your query to not return a row you don't need but you can just remove the first item from the array:
<?php
array_shift($values);
foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>
See http://uk1.php.net/array_shift

<?php
unset($values[0]);
foreach ($values as $value) { ?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php } ?>

If you want to exclude url pattern than you should use regex match like this:-
<?php
foreach ($values as $value)
{
if(!preg_match('#^(http|https)?://#', $value))
{
?>
<span><?php echo cimy_uef_sanitize_content($value['VALUE']); ?></span>
<?php
}
}
?>
Hope this will help you to exclude urls.

Related

Remove this section of code and put it into another loop

I've been racking my brain for weeks trying to remove this block of code, it's from paid memberships pro plugin.
I need to remove the below section as it produces the file fields on the user profile.
need to remove it and put it in a loop of its own and then print it, as I need it to be in a separate <section> from the rest of the fields that are in that original loop.
//----Trying to remove this piece of code----//
?>
<strong><?php echo $field[0]; ?></strong>
<div class="Test"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
//---This is the FullSnippet with the above code included----//
if(!empty($fields_array))
{
foreach($fields_array as $field)
{
if(empty($field[0]))
break;
// Fix for a trailing space in the 'fields' shortcode attribute.
if ( $field[0] === ' ' ) {
break;
}
$field_val = $field[1];
$meta_field = $pu->$field_val;
if(!empty($meta_field))
{
?>
<section class="pmpro_member_directory_<?php echo esc_attr($field[1]); ?>">
<?php
if(is_array($meta_field) && !empty($meta_field['filename']) )
{
//this is a file field
?>
<strong><?php echo $field[0]; ?></strong>
<div class="brendan"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
}elseif(is_array($meta_field)){
//this is a general array, check for Register Helper options first
if(!empty($rh_fields[$field[1]])) {
foreach($meta_field as $key => $value)
$meta_field[$key] = $rh_fields[$field[1]][$value];
}
?>
<strong><?php echo $field[0]; ?></strong>
<ul class="servicesList"><?php echo '<li>'.implode("</li>\n<li>",$meta_field).'</li>';?></ul>
<?php
}
elseif(!empty($rh_fields[$field[1]]) && is_array($rh_fields[$field[1]]) )
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo $rh_fields[$field[1]][$meta_field]; ?>
<?php
}
else
{
if($field[1] == 'user_url')
{
?>
<?php echo $field[0]; ?>
<?php
}
else
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php
$meta_field_embed = wp_oembed_get($meta_field);
if(!empty($meta_field_embed)){
echo $meta_field_embed;
}else{
echo make_clickable($meta_field);
}
?>
<?php
}
}
?>
</section>
<?php
}
}
}
?>

Remove comma on last item of foreach

I have next code for WordPress loop of tags for single post:
<?php if ($tags) : foreach($tags as $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>,
<?php endforeach; endif; ?>
I have comma added to last anchor. There is also white space after comma.
How can I remove the comma from last anchor while I am doing this with foreach() PHP loop?
Thanks for ideas and help!
Check if your loop is working on the last one:
<?php if ($tags) : ?>
<?php $count = count($tags); ?>
<?php foreach($tags as $i => $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php if ($i < $count - 1) echo ", "; ?>
<?php endforeach; ?>
<?php endif; ?>
What has a higher cost, calling a function or setting a variable? Here's another way to do it perhaps, which sets a variable and removes the offending chars at the end - no extra math or if checks needed.
<?php
$tagoutput = '';
if ($tags) {
foreach ($tags as $tag)
$tagoutput .= '' . $tag->name . ', ';
$tagoutput = rtrim($tagoutput, ', ');
}
echo $tagoutput;
?>
You can do it the other way around (remove it from the first one). If your array is numeric you can try something like this:
<?php if ($tags): ?>
<?php foreach ($tags as $key => $tag): ?>
<?php if ($key > 0): ?>,<?php endif ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php endforeach ?>
<?php endif ?>
You can also try it using counter.
$values = array('value','value','value');
$count = count($values);
$i = 0;
foreach($values as $value){
$i++;
echo $value;
if($count > $i){
echo ', ';
}
}
Output: value, value, value

How to perform a count in a loop

foreach ($arrQuestionId as $key=>$question) {
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:</strong></p>
</div>
<?php
}
?>
In code above I have a while loop where it displays QUESTION :. Now what I want to do is in between QUESTION and : I want to include a count number so that for every time QUESTION appears, it contains a count number next to it as like below:
QUESTION 1:
QUESTION 2:
QUESTION 3:
...
How can this be done?
Create a variable and increment it in foreach.
Like:
$s = 1;
foreach($value as $text) {
echo "Question #".$s." :".$text;
$s++;
}
Take a variable and increment it
$i=1;
foreach ($arrQuestionId as $key=>$question) {
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"><? echo $i; ?></span>:</strong></p>
</div>
<?php
$i++;
}
?>
$count = 1;
foreach ($arrQuestionId as $key=>$question) {
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"><?php echo $count++; $></span>:</strong></p>
</div>
<?php
}
?>
$i=0;
foreach ($arrQuestionId as $key=>$question) {
$i=$i+1;
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:<?php echo $i;?></strong></p>
</div>
You can try the following way too,
$countArray = array();
foreach ($arrQuestionId as $key=>$question) {
$countArray[] = $key;
}
count($countArray) or sizeOf()
(OR) Directly you can get without for loop itself like the below
count($arrQuestionId)
$qno = 1;
foreach ($arrQuestionId as $key=>$question) {
?>
<div class='lt-container'>
<p><strong>QUESTION <span id="quesnum"></span>:</strong><?php echo $qno?></p>
</div>
<?php
$qno++;
}
?>

Looping through a PHP array

I have the following PHP array structure:
$set = array();
$set[] = array('firstname'=>'firstname 1',
'lastname'=>'lastname 1',
"bio"=>array('paragraph 1 of the bio, 'paragraph 2 of the bio','paragraph 3 of the bio'),
);
I then access the array with the following:
<?php $counter = 0;
while ($counter < 1) : //1 for now
$item = $set[$counter]?>
<h1><?php echo $item['firstname'] ?></h1>
<h1><?php echo $item['lastname'] ?></h1>
<?php endwhile; ?>
I'm uncertain how I can loop through the "bio" part of the array and echo each paragraph.
So as a final output, I should have two h1s (first and last name) and three paragraphs (the bio).
How can I go about doing this?
Use foreach loop
foreach($item['bio'] as $listitem) {
echo $listitem;
}
You don't need to use a manual counter, you can use foreach. It's generally the easiest way and prevents off-by-one errors.
Then you need a second inner loop to loop through the bio.
<?php foreach ($set as $item): ?>
<h1><?php echo $item['firstname'] ?></h1>
<h1><?php echo $item['lastname'] ?></h1>
<?php foreach ($item['bio'] as $bio): ?>
<p><?php echo $bio; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
On a related note; you probably want to look into escaping your output.
Add into the while loop also this:
<?php foreach ($item['bio'] as $paragraph): ?>
<p><?php echo $paragraph; ?></p>
<?php endforeach; ?>
Note that used coding style is not optimal.
Try:
foreach($set as $listitem) {
if(is_array($listitem)) {
foreach($listitem as $v) //as $set['bio'] is an array
echo '<h1>' .$v . '</h1>';
} else
echo '<p>'.$listitem.'</p>';
}

Echo an Array Key.. how?

Im looking to echo the value of my array key out into the view for this controller. I cant quite figure it out. So for the first feed I want to echo mmafighting into the of the view. Then it would loop through the rest.
Here is my controller code:
function index()
{
$this->load->library('simplepie');
$feeds = array('mmafighting' => 'http://www.mmafighting.com/rss/current',
'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml',
'bloodyelbow' => 'http://www.bloodyelbow.com/rss',
'ufc' => 'http://www.ufc.com/rss/news',
'hackernews' => 'http://news.ycombinator.com/rss',
'msnbc' => 'http://www.msn.com/rss/news.aspx',
'msnbc2' => 'http://www.msn.com/rss/msnmoney.aspx',
'msnbc3' => 'http://www.msn.com/rss/msnshopping_top.aspx'
);
foreach ($feeds as $site=>$url)
{
$data['feed'][$site] = new SimplePie();
$data['feed'][$site]->set_feed_url($url);
$data['feed'][$site]->set_cache_location(APPPATH.'cache');
$data['feed'][$site]->set_cache_duration(300);
$data['feed'][$site]->init();
$data['feed'][$site]->handle_content_type();
}
$this->load->view('feedsview', $data);
}
Here is my view code:
<?php
$feedCount = 0;
$rowCount = 0;
?>
<?php foreach ($feed as $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php // echo original array key here ?></h2>
<ul>
<?php $items = $site->get_items(0, 5); ?>
<?php foreach ($items as $item): ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php $feedCount++; ?>
<?php if ($feedCount === 3) {
echo '<div style="clear:both;"> </div>';
$feedCount = 0;
$rowCount++;
if ($rowCount === 2) {
echo '<div style="width:1000px; margin-bottom:20px;height:100px; border:1px solid #252525; float:left;">images</div>';
}
} ?>
<?php endforeach; ?>
I'm not sure i understand but what about doing this :
<?php foreach ($feed as $key => $site): ?>
<?php if ($site->data): ?>
<div class="feed">
<h2><?php echo $key ?></h2>
<ul>

Categories