Listing number of rows by group - php

I have a database with members joining and I am trying to display the number of members from each country.
Here is the code I'm using to show the country flags:
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $countrynum . ' fans<br>';
}
?>
I have really looked for the answer, and keep seeing the same type of code, but I haven't been able to get it to work for me.
I've tried this:
$countrynum = SELECT COUNT(name) FROM $table WHERE country = $sortvalue;
which I inserted after the foreach statement. This (and every other iteration of this I've tried gives me Parse error: syntax error, unexpected 'COUNT' (T_STRING) in...
The closest I've come to solving this is by using this code (though, I shouldn't have to reconnect to the db should i?):
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT country, COUNT(*) FROM $table GROUP BY country') as $row) {
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fans<br>';
}
?>
This gave me the correct row (member) count, but the same single flag was displayed beside each number instead of the corresponding flag with number of members.
I'm sure this is probably super simple for lots of you, but I'm a newbie who's really trying to learn coding and so far that mostly means cutting, pasting and LOTS of trial and error.
Any assistance (and learning resource suggestions) would be very much appreciated. :)

Since the flag image source is depending on$sorters and $sortvalue, it will be same for all country. Look at the loops. For each $sorters as $sortvalue, the link will be multiple countries but the img source will be same for all.

Related

Last posts text limit characters in phpbb

I found this code for showing last posts in phpbb forum. I wanna set charatcters limit in post_text.
<?php
// Now let's output the content
// A ted vypsat obsah
while ($row = $db->sql_fetchrow($result))
{
$url = generate_board_url() . "/viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}"; //added fedforum to url
$urlmini = generate_board_url() . "/memberlist.{$phpEx}?mode=viewprofile&u={$row['poster_id']}"; //added fedforum to url
//old line $url = generate_board_url() . "viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}";
echo '<small><a target="_blank" href="' . $url . '">' . $row['post_subject']. '</a><br>'. $row['post_text'] .'<br>od: <a target="_blank" href="' . $urlmini . '">' . ucwords($row['username']).'</a>' . ' v '.'<font style="color:#aaa;">' . date("H:i",$row['post_time']).'</font>', '<br><br></small>';
}
?>
Does anybody know how to set characters limit for $row['post_text'] ?
you can use
substr($row['post_text'],0,40);
or change 40 to any limit as you want

How would I add another column and populate it?

I know this is way out of left field, but i was wanting to see if anyone could help.
I am wanting to add a column, populating the data from a certain variable (the variable i have yet to discover the name of). This is a plugin for wordpress and the developer wont really help.
Last ditch effort i guess. Here is the code:
}$output.='><tr><th>' . __('Request For', 'wpsc-support-tickets') . '</th><th>'
. __('Status', 'wpsc-support-tickets') . '</th><th>'
. __('Last Reply', 'wpsc-support-tickets')
. '</th><th>' . __('Department', 'wpsc-support-tickets') . '</th></tr>';
I added the last column, "Department".
Now, looking at the following code, i cant figure out how to populate that data (assuming i knew the variable even)
$output .= '<tr><td>
<a href="" onclick="loadTicket(' . $result['primkey'] . ',\'' . $canReopen . '\');
return false;" ';
if ($result['resolution'] == strtolower('open') ) {
$resresolution = __('Open', 'wpsc-support-tickets');
} elseif ($result['resolution'] == strtolower('closed') ) {
$resresolution = __('Closed', 'wpsc-support-tickets');
} else {
$resresolution = $result['resolution'];
}
if ($devOptions['disable_inline_styles'] == 'false') {
$output.='style="border:none;text-decoration:none;"';
}$output.='><img';
if ($devOptions['disable_inline_styles'] == 'false') {
$output.=' style="float:left;border:none;margin-right:5px;"';
}$output.=' src="' . plugins_url('/images/page_edit.png', __FILE__) . '"
alt="' . __('View', 'wpsc-support-tickets') . '" /> ' . base64_decode($result['title']) .
'</a></td><td>' . $resresolution . '</td><td>'
. date_i18n( get_option( 'date_format' ),
$result['last_updated']) . ' ' . __('by', 'wpsc-support-tickets') . '
' . $last_staff_reply . '</td>
</tr>';
again - where might i add the variable for the column "department" if the variable were $department_var?
You can add it to the very end of that large chunk of mess before the closing </tr>...
change
...$last_staff_reply . '</td></tr>';
to
...$last_staff_reply . '</td><td>' . $department_var . '</td></tr>';

Call specific variable based on what another variable is set to.(PHP)

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.
I am trying to make a variable name itself based the value of another.
I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.
Here is a better description.
This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.
extract(shortcode_atts(array(
'size' => 'medium',
'link' => '#',
'text' => ''
), $atts));
$large_button_img = of_get_option('large_button_arrow_upload');
$button_pos = of_get_option('button_image_position');
if($button_pos == 'right' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>";
}elseif($button_pos == 'left' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>";
}else
{
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
In the above:
The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size...
At the moment I have it to where the user can set different images for a large, medium and small button.
Question:
IS IT POSSIBLE
to return the name of the image source based on what size is set to.
soooo basically the equivalent of?
img src = '" . $($size)_button_img . "'
Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like
img src= '" . $large_button_img ."'
or, if the user has medium selected
img src= '" . $medium_button_img . "'
If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.
Thanks in advance for any help that can be given :)
ALSO
Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.
You can simple use function for your logic:
function somethingWithImage($img, $pos) {
if ($pos == 'right' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>";
} elseif ($pos == 'left' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>";
} else {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
}
And just call it whenever you want with any arguments.
What you are looking for, are variable variable names.
Short example:
$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';
$varname = 'hello';
echo ${$varname}_text; // Hello World!
// Is the same as:
echo $hello_text; // Hello World!
As I mentioned, you can use interpolation to create variable variables:
$src = ${"{$size}_button_img"};
But may I suggest using arrays instead? You get cleaner and understandable code:
$sizes = array(
'large' => ...,
'medium' => ...,
);
if(isset($sizes[$size]))
$src = $sizes[$size];

google checkout php callback explained

Hi all i don't know if i'm doing this right or not but i can't understand how google checkout works.
I have the php code that sends the XML to the google checkout everything is ok i reach on the checkout process taxes prices shipping is ok.
$_currency = $params['currency'];
$base_domain = 'https://' . (($params['mode'] == 'transaction') ? 'checkout.google.com' : 'sandbox.google.com/checkout');
$base_url = $base_domain . '/cws/v2/Merchant/' . $params['merchantId'] . '/checkout';
// Form XML array with cart items
$_items = '';
$google_products = $this->CI->go_cart->contents();
foreach ($google_products as $k => $v) {
$item_options = '';
if (isset($v['options'])) {
foreach ($v['options'] as $name => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$item_options .= $item;
}
} else {
$item_options .= $value;
}
}
}
$_items .= '<item>' . '<merchant-item-id>' . $v['id'] . '</merchant-item-id>' . '<item-name>' . strip_tags($v['name']) . ' ' . $item_options . '</item-name>' . '<item-description>' . substr(strip_tags($item_options), 0, 299) . '</item-description>' . "<unit-price currency='" . $_currency . "'>" . $v['price'] . '</unit-price>' . '<quantity>' . $v['quantity'] . '</quantity>' . '</item>';
}
$xml_cart = "<?xml version='1.0' encoding='UTF-8'?>
<checkout-shopping-cart xmlns='http://checkout.google.com/schema/2'>
<shopping-cart>
<merchant-private-data>
<additional_data>
<session_id>" . base64_encode($customer['bill_address']["email"] . date('h:i:s')) . "</session_id>
<currency_code>" . $_currency . "</currency_code>
<payment_id>" . base64_encode($customer['bill_address']["email"] . date('h:i:s')) . "</payment_id>
" . $private_ship_data . "
</additional_data>
</merchant-private-data>
<items>" . $_items . "</items>
</shopping-cart>
<checkout-flow-support>
<merchant-checkout-flow-support>
<platform-id>971865505315434</platform-id>
<request-buyer-phone-number>true</request-buyer-phone-number>
<edit-cart-url>" . $edit_cart_url . "</edit-cart-url>
<merchant-calculations>
<merchant-calculations-url>" . $calculation_url . "</merchant-calculations-url>
</merchant-calculations>
<continue-shopping-url>" . $return_url . "</continue-shopping-url>
" . $shippings . $taxes . "
</merchant-checkout-flow-support>
</checkout-flow-support>
</checkout-shopping-cart>";
$signature = $this->fn_calc_hmac_sha1($xml_cart, $params['merchantKey']);
$b64_cart = base64_encode($xml_cart);
$b64_signature = base64_encode($signature);
echo '<form method="post" action="' . $base_url . '" name="BB_BuyButtonForm">
<input type="hidden" name="cart" value="' . $b64_cart . '" />
<input type="hidden" name="signature" value="' . $b64_signature . '" />
<input alt="" src="' . $base_domain . '/buttons/checkout.gif?merchant_id=' . $params['merchantId'] . '&w=160&h=43&style=' . 'white' . '&variant=text&loc=en_US" type="image"/>
</form>';
exit;
}
After i click on submit payment i'm then redirected to a page with successfull message and with a link to return to my website.
I'm wondering how can i check if the payment was or not successfully.
Also there is a callback that i don't know exactly what is supposed to do.
Please help me understand better
I have attached the code below.
Disclaimer: Am not a PHP dev.
Your code above is known as Part 1. Checkout API documented here. It covers how you get your data/cart to Google for Checkout
Part II. Order Processing covers how to get data from Google (into your systems). A tutorial is provided here. This is where you provide a Callback API URL that listens to (waits for) HTTP POSTs from Google and proceed to process the data.

Inside of a ternary php script how do i concatenate a second variable into my string?

Before style="style="color: ' . $rx_event_colors . '" I want to add the $ima variable. it's just a variable with an image inside of the string. Additionally, if you think i would benefit from just adding the
if(!empty($ins_event))
{
echo "<tr><td> <a href='". matry::here(array('event_id'=>$ins_event['id'])) . "'" .
( $ins_event['status'] == 2 ? ' style="color: ' . $rx_event_colors . '">Completed Insurance Event' : '>Active Insurance Event') . "</a></td></tr>";
}
I've tried:
? ' $ima, style="style="color: ' . $rx_event_colors . '"
style="style="color: ' . $ima, $rx_event_colors . '"
style="style="color: ' . ($ima), $rx_event_colors . '"
style="style="color: ' . ($ima), ($rx_event_colors) . '"
with no avail.
I'm assuming $ima is a string. To concatenate strings, use the "."
So in the first part of your ternary, if you'd like to add $ima, simply do this:
' style="color: ' . $ima . $rx_event_colors . '">Completed Insurance Event'
However, I'm confused when you say "[$ima] just a variable with an image inside of the string". Does $ima contain the path to the image itself? If so, it belongs in the SRC attribute of the img tag, not inside the STYLE attribute.
UPDATE:
Because $ima contained the entire img tag, it doesn't belong in the style atrribute. See:
' style="color: ' . $rx_event_colors . '">' . $ima . 'Completed Insurance Event'

Categories