Need help in printing records from database - database

I want to print records from db like this.
http://www.imagesup.net/dm-613781138202.png
I have tried for loop and foreach both.
The sample code is:
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
$str2 = (explode(",",$str));
echo '<table border="1">';
foreach ($str2 as $str3)
{
echo '<tr>';
for($i=0;$i<5;$i++)
{
echo '
<td>'.$str3.'</td>
';
}
echo '</tr>';
}
echo '</table>';
?>
I have tried many others but not getting required result.

#kami you should replace + with . like this.
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
echo '<table>';
$list = explode(",", $str);
$itemsPerRow = 7;
for ($i = 0; $i < sizeof($list); $i+=$itemsPerRow)
{
echo '<tr>';
for($j = 0; $j < $itemsPerRow; $j++)
{
$val = isset($list[$i + $j]) ? $list[$i + $j] : '';
echo '<td>' .$val. '</td>';
}
echo '</tr>';
}
echo '</table>';
?>

Try something like this
<?php
$str = '1,2,3,4,5,6,7,8,9,10,11,12,13,14';
echo '<table>';
$list = explode(",", $str);
$itemsPerRow = 7;
for ($i = 0; $i < sizeof($list); $i+=$itemsPerRow)
{
echo '<tr>';
for($j = 0; $j < $itemsPerRow; $j++)
{
$val = isset($list[$i + $j]) ? $list[$i + $j] : '';
echo '<td>' . $val . '</td>';
}
echo '</tr>'
}
echo '</table>'
?>
The above uses two for loops to iterate over the data. The outer loop controls the row, and the inner loop controls the content.

Related

add a span tag to each letter, space, character from title

I try to add span tag to each letter, space, character from title. When I use str_split then special characters are converted to another format.
if ( !function_exists( 'my_title' ) ):
function my_title( $str = '' ) {
$output = '';
$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;
if ( empty( $str ) ) {
return $output;
}
$i = 1;
foreach( str_split ( $str ) as $letter ) if ( $i++ <= 12 ) {
if ( !empty( $letter ) ) {
$output .= '<span>' . $letter . '</span>';
}
};
return wp_kses_post( $output );
}
endif;
echo my_title('Page title - with dash');
The problem is likely because of the wptexturize() function which is being applied on post titles, where certain characters such as - (dashes) are automatically converted to HTML entities such as – for "beautiful" dashes.
So try this, which worked for me:
if ( !function_exists( 'my_title' ) ):
function my_title( $str = '', $max_chars = 12 ) {
$output = '';
remove_filter( 'the_title', 'wptexturize' );
$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;
add_filter( 'the_title', 'wptexturize' );
if ( empty( $str ) ) {
return $output;
}
$str = html_entity_decode( $str, ENT_NOQUOTES, 'UTF-8' );
for ( $i = 0; $i < min( mb_strlen( $str ), $max_chars ); $i++ ) {
if ( $letter = mb_substr( $str, $i, 1 ) ) {
$output .= '<span>' . $letter . '</span>';
}
}
return wp_kses_post( $output );
}
endif;
UPDATE — Notes:
Sorry #michael, I should've mentioned that I also modified the my_title() function to use the html_entity_decode(), mb_strlen(), and mb_substr() functions, so that HTML entities are properly handled. (str_split() is not able to properly handle multi-byte encoded string — for example, HTML entities such as the –)
Hence, even if the wptexturize() function is applied on the post title, the modified my_title() function should have no problems of handling the – and other HTML entities (or special characters like that). So you can, if you want, change this:
remove_filter( 'the_title', 'wptexturize' );
$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;
add_filter( 'the_title', 'wptexturize' );
to this:
$str = empty( $str ) ? wp_strip_all_tags( get_the_title() ) : $str;
i.e. it's not necessary to remove the wptexturize hook. Just in case if you'd like to keep the "beautiful" dashes and other special characters converted by the wptexturize() function. =)

CodeIgniter database error (SUM)

Hello i've problem like this
this is my model:
$sql = "SELECT saldo.kode, saldo.nama, SUM(bayar.jmlh_bayar) FROM saldo, bayar WHERE saldo.nama = bayar.akun ORDER BY saldo.nama";
$result = $this->db->get($sql);
return $result->result();
this is controller:
public function show()
{
$this->load->model('uas/sampah_model');
$terserah['c']= $this->sampah_model->get();
//$terserah['kode']=$this->sampah_model->kode();
$this->load->view('UAS/saldo',$terserah);
}
this is my view:
<tr>
<?php
foreach ($c as $item){
echo "<td>";
echo $item->kode;
echo "</td>";
echo "<td>";
echo $item->nama;
echo "</td>";
echo "<td>";
echo $item->jmlh_bayar;
echo "</td>";
echo "<td>";
echo anchor('UAS/utama/hapus/'.$item->kode,'Hapus');
echo "</td>";
echo "</tr>";
}
?>
Can anybody help me?
Hope this will help you :
Instead of get use query
Your query should be like this :
$sql = "SELECT saldo.kode, saldo.nama, SUM(bayar.jmlh_bayar)
FROM saldo, bayar WHERE saldo.nama = bayar.akun
ORDER BY saldo.nama";
$result = $this->db->query($sql);
return $result->result();

Communicating with a DB in LimeJS

What is the proper way to load variables into my game from a DB?
I tried using Ajax and the Prototype library, but that doesn't seem to work. Here's what I did:
In my main .js game file...
var rawVocab = new Array();
var optionVocab = new Array();
new Ajax.Request('load_vocab.php', {
onSuccess : function(xmlhttp) {
eval(xmlhttp.responseText);
}
});
'load_vocab.php' looks like this...
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$username = "user";
$password = "***************";
try {
$conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM vocabulary_game WHERE game_type = :game_type');
$stmt->execute(array('game_type' => 'target'));
$i=0;
while($row = $stmt->fetch()) {
echo "rawVocab[".$i."]['word']='".$row['word']."';";
echo "rawVocab[".$i."]['translation']='".$row['translation']."';";
echo "rawVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
$stmt = $conn->prepare('SELECT * FROM vocabulary_game');
$stmt->execute(array());
$i=0;
while($row = $stmt->fetch()) {
echo "optionVocab[".$i."]['word']='".$row['word']."';";
echo "optionVocab[".$i."]['translation']='".$row['translation']."';";
echo "optionVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</response>';
?>
Is there some built in way to handle this with the goog library?
Apparently, Google Closure has a built-in way (goog.net.XhrIo) to handle Ajax calls.
1 - http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
2 - http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/

cakePHP: how to get iterator

Im using something similar to this code:
<?php foreach ($items as $item): ?>
<div><?php echo $item['Item']['content']; ?></div>
<?php endforeach; ?>
And i'd like to know which element is every item, because i want to add class "last-in-row" for every fourth item in row. How to make something like this code?
<?php for ($i=1; $i <= $items.count; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">$items[$i]</div>";
}; ?>
I haven't tested it but the following should work.
<?php
$i = 1;
foreach ($items as $item) {
$class = ($i % 4 == 0) ? '' : 'class="last-in-row"';
echo "<div $class>{$item['Item']['content']}</div>";
$i++;
}
?>
p.s. I hope you are sanitizing $item['Item']['content'].
What you are trying to do could be done with css3. This will mean you do not need to add a class which is fat better as later on you might want 3 or 5 in a row.
div:nth-child(4n+4) {
....
clear: both;
....
}
The CakePHP option without css3
foreach ($items as $i => $item) {
echo $this->Html->tag('div', $item['Item']['content'], array(
'class' => ($i + 1) % 4 === 0 ? 'last' : null
));
}
You nearly had it in your second example, just change $items.count to the value of count($items)
$itemsCount = count($items);
for ($i=1; $i <= $itemsCount ; $i++) {
echo "<div ";
if ($i % 4 == 0) {
echo " class=\"last-in-row\""; }
echo ">{$items[$i]}</div>";
}

Array placeholders

I’ve got an array where PLACEHOLDER is a placeholder for the variable $value that I get later in the code:
$names = array(
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://hank.com' title='PLACEHOLDER'>Hank</a>",
"<a href='http://marie.com' title='PLACEHOLDER'>Marie</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
);
To check how often an equal value is in my array,
I count them with array_count_values.
$count = array_count_values($names);
foreach ($count as $key => $value) {
echo $value . ' – ' . $key . '<br />';
}
So I get something like this:
3 – <a href='http:/walter.com' title='PLACEHOLDER'>Walter</a>
2 – <a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>
2 – <a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>
1 – <a href='http://hank.com' title='PLACEHOLDER'>Hank</a>
1 – <a href='http://marie.com' title='PLACEHOLDER'>Marie</a>
Now I’d PLACEHOLDER be replaced by $value, so I get the number as title tag of the link.
foreach ($count as $key => $value) {
echo $value . ' – ' . str_replace('PLACEHOLDER', $value, $key) . '<br />';
}
What are you asking, I don't understand, you want to replace PLACEHOLD with $value then do this
$names = array(
"<a href='http://skyler.com' title='".$PLACEHOLDER."'>Skyler</a>",
"<a href='http://jesse.com' title='".$PLACEHOLDER."'>Jesse</a>"
);
Use str_replace to replace PLACEHOLDER with $value
foreach ($count as $key => $value) {
$key = str_replace('PLACEHOLDER', $value, $key); //<--replace PLACEHOLDER here
echo $value . ' – ' . $key . '<br />';
}
Correct code is :
foreach ($count as $key => $value) {
echo str_replace('PLACEHOLDER', $value, $key); //this will replace placeholder with number of tags
echo '<br />';
}

Resources