Hi I want to add content after every 3rd div in the loop. Here is below code but I am not getting even render content "Hi this is the 3d div"
Its not detecting every 3rd div.
<?php
function q_list_item($q_item)
{
$count = 0;
$this->output('<DIV>');
$this->my_items;
$this->output('</DIV>');
$count++;
if($count % 3 == 0) {
echo 'Hi this is the 3rd div';
}
}
?>
----[Actual Function]-----------------------------------------------
<?php
function q_list_item($q_item)
{
$this->output('<DIV CLASS="qa-q-list-item'.rtrim(' '.#$q_item['classes']).'" '.#$q_item['tags'].'>');
$this->q_item_stats($q_item);
$this->q_item_main($q_item);
$this->q_item_clear();
$this->output('</DIV> <!-- END qa-q-list-item -->', '');
}
?>
You are resetting the $count to 0 at the top of this function, so it will always be 1 when you run the if statement at the end of the function.
This may help with your issue, although I can't tell if your code is in a class or not as it doesn't look like it is, but you are using $this-> in there. Essentially, move the instantiation of the counter outside of the function:
<?php
$q_list_count = 0;
function q_list_item($q_item)
{
$q_list_count++;
$this->output('<DIV>');
$this->my_items;
$this->output('</DIV>');
if($q_list_count % 3 == 0) {
echo 'Hi this is the 3rd div';
}
}
?>
<?php
$count = 0;
function q_list_item($q_item)
{
$this->output('<DIV>');
$this->my_items;
$this->output('</DIV>');
$count++;
if($count % 3 == 0) {
echo 'Hi this is the 3rd div';
}
}
?>
initialize the $count outside the loop, otherwise count will always be 1 when it reaches the if statement
Related
I am currently working on a project (A music website) with PHP Codeigniter. I want to fetch top 15 songs by number of downloads. To my knowledge, I got all unique ID's out ranging from highest to least. But now, I want to use each of these unique ID's to fetch data from another table in the database.
I have tried using for loop; but it seems to only return one of the rows which happens to be the first and highest number of occurrence in the array. The code below echo's the ID's but how can I use each of those ID's to fetch data.
function top15(){
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row)
{
$musiccode[] = $row['musiccode'];
}
$mode = array_count_values($musiccode);
arsort($mode);
$i = 0;
foreach ($mode as $field => $number) {
$i++;
echo $field." occured ". $number." times <br>";
if ($i == 15) {
break;
}
}
}
}
for ($b=0; $b < count($field); $b++) {
$this->db->where(array('track_musiccode'=> $field));
$field = $this->db->get('tracks');
return $field->result_array();
}
The code above only produce one row for me. I expect to have more than one and according to the number of ID's in the array. Thanks in advance.
For a single column condition, you could use where_in() instead and drop the for loop :
$this->db->where_in('track_musiccode', $field);
$field = $this->db->get('tracks');
return $field->result_array();
Yes Finally i did it. Incase if anyone needs it. this works just fine
function top15(){
$this->db->where('d_month', date('m'));
$this->db->select('musiccode');
$result=$this->db->get('downloads');
$query = $result->result_array();
$downloads = array();
if(!empty($query)) {
foreach($query as $row){
$musiccode[] = $row['musiccode'];
}
$res='';
$count=array_count_values($musiccode);
arsort($count);
$keys=array_keys($count);
for ($i=0; $i < count($keys); $i++) {
$res[] .= "$keys[$i]";
}
$this->db->where_in('track_musiccode', $res);
return $this->db->get('tracks')->result_array();
}
}
And I got this too. If I wasn't going to count occurrences but just iterate the number of downloads which is far more better than counting in other not to over populate the database and make your website work slowly.
function top15(){
$tracks = '';
$this->db->group_by('counter');
$result=$this->db->get('downloads');
$query = $result->result_array();
$counter = '';
$fields = '';
if(!empty($query)) {
foreach($query as $row) {
$counter[] = $row['counter'];
}
rsort($counter);
for ($i=0; $i<=15; $i++) {
$this->db->where('downloads.counter', $counter[$i]);
$this->db->join('tracks', 'tracks.musicid = downloads.musicid');
$fields[] = $this->db->get('downloads')->row_array();
}
return $fields;
}
}
Enjoy...
In order to print_r a collapsable tree, I'm currently using his code which uses preg_replace() and the /e modifier, which is depreciated in PHP7:
<?php
function print_r_tree($data)
{
// capture the output of print_r
$out = print_r($data, true);
// replace something like '[element] => <newline> (' with ...<div id="..." style="display: none;">
$out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1\\2<div id=\"'.\$id.'\" style=\"display: none;\">'", $out);
// replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
$out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);
// print the javascript function toggleDisplay() and then the transformed output
echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}
?>
In order to fix it, I tried this solution Preg replace deprecated, attempting to fix
but it's not working. (Yes, I recognized the ';' is missing in the function there).
Since - due to lack of 'reputation' points - I can't comment there, I'm trying to get an answer here...
This is the unchanged proposed solution for 2nd $out in the link:
$out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', "callbackFunction", $out);
function callbackFunction($matches) {
return "'".$matches[1]."".$matches[2]."<div id=\"'.\$id.'\" style=\"display: none;\">'"
}
You need to concat the variable $id value, try this:
function callbackFunction($matches) {
$id = substr(md5(rand().$matches[0]), 0, 7);
return "$matches[1]$matches[2]<div id='$id' style=\"display: none;\">'";
}
What I'm trying to achieve:
Show either the post tag (without the link) or the title.
If a tag exists, that gets printed if it doesn't, the title gets used.
Issue: I've used get_the_tags() as suggested in the Codex to get the tag without the link and that works, yet it gets the word "Array" printed as a preffix, too.
<?php
if( has_tag() )
{
echo $posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
What am I missing?
You are echo ing $posttags which is an array. If you echo an array it will echo array as output
<?php
if( has_tag() )
{
This is printing Array as prefix ----> echo $posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
Please remove that echo , so your new code will be
<?php
if( has_tag() )
{
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
}
else { echo the_title(); };
?>
Hope this helps you
I have a basic code here:
<?
include("inc_dblib.php");
include("inc_ecs.php");
$db = dbconnect();
$id = 10;
?>
<?php echo text_view($db,$id,"<h3><br />^lead^</h1> <br />^text^");?>
<br />
inside inc_ecs.php i have:
function text_view($dblink,$id,$code) {
if( !$rset = dbquery($dblink,"article_view",$id) )
return FALSE;
$item = mysql_fetch_assoc($rset);
$text=$item["text"];
$title=$item["title"];
$lead=$item["lead"];
$capelo=$item["capelo"];
$author=$item["author"];
$vowels = array("^text^","^title^","^capelo^","^lead^", "^author^");
$yummy = array($text, $title, $capelo, $lead,$author);
$code = str_replace($vowels,$yummy,$code);
return $code;
}
however every time I run my script it tells me
Fatal error: Call to undefined function text_view
. Any ideas? Thanks.
Ok
so I found an other problem.
I have tried to insert the echo "Hello World!"; in the code of the inc_ecs.php.
When I browsed the page, I realised that a major part the code it is shown as text.
I turned back to the remote version and it shows a blank page when called in a browser.
the inc_ecs.php page start showning the code from "return $outputVar;" and the remaining hole code of the page is shown:
function graphical_counter ($db, $id){
$str = counter($db, $id);
$visitors_split = chunk_split ($str,1,'');
$visitors = strlen($str);
for ($i ; $i< $visitors ; $i++){
$outputVar .= "<img src='./images/counter/".$visitors_split[$i].".gif' width='15' height='20' border='0' align='absmiddle'>";
}
return $outputVar;
}
/*
End Counter Functions
Is there an error in this code ?
your include(); should be inside of the tag:
<?php
include("inc_ecs.php");
echo text_view($db,$id,"<h3><br />^lead^</h1> <br />^text^");
?>
I have created pagination and it is currently working, but the only issue is if I have thousands and thousands of results... the display would be:
[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18] and so on.
I just want the display the page to display to look similar to this:
previous [1][2][3][4][5]... next
previous ...[5][6][7][8][9][10]...next
Can anyone give advice or supply some example code that would give me a result like above? If anything is at all unclear please let me know!
Cheers,
Neil
The code that I am currently using is below:
<?php
/*data base connection */
include "datebase connection";
/* SQL query */
$tsql = (" SELECT TOP 100 tie_parent_id, CAST(geo_post AS varchar(6)) + '.' + CAST(geo_sample AS varchar(6)) AS Mile, gps_lat, gps_long, rotten, split, wheel_cut, broken, quality
FROM database
");
$stmt = sqlsrv_query($conn,$tsql, array(), array( "Scrollable" => 'static'));
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* DETERMINING THE NUMBER OF ROWS (AND PAGES) */
// Set the number of rows to be returned on a page.
$rowsPerPage = 10;
// Get the total number of rows returned by the query.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{
/* Calculate number of pages. */
$numOfPages = ceil($rowsReturned/$rowsPerPage);
}
/* FUNCTION FOR PAGING */
function getPage($stmt, $pageNum, $rowsPerPage )
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while($row = sqlsrv_fetch_array($stmt,
SQLSRV_FETCH_NUMERIC,
SQLSRV_SCROLL_ABSOLUTE,
$offset + $i)
&& $i < $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
$row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE, $offset -1);
return $rows;
}
// Display the selected page of data.
echo "<table width='800' border='0'>";
echo "<tr> <th>Tie ID</th> <th>Mile/Yard</th> <th>GPS Lat</th><th>GPS Long</th><th>Rotten</th><th>Split</th><th>WheelCut</th> <th>Broken</th><th>Quality</th> </tr>";
// keeps getting the next row until there are no more to get
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);
$color1 = "#ffffff";
$color2 = "#edf5fa";
$row_count = "0";
while($row_count<10 ) {
$row=sqlsrv_fetch_array($stmt);
$tie_parent_id = $row["tie_parent_id"];
$geo_post = $row["Mile"];
$lat =$row["gps_lat"];
$long =$row["gps_long"];
$rotten =$row["rotten"];
$split =$row["split"];
$wheelcut =$row["wheel_cut"];
$broken =$row["broken"];
$quality =$row["quality"];
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["tie_parent_id"]; ?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["Mile"];?> </td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["gps_lat"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["gps_long"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["rotten"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["split"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["wheel_cut"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["broken"];?></td>
<td bgcolor="<?php echo $row_color ?>">
<?php echo $row["quality"];?></td>
</td></tr>
<?php
$row_count++;
}
?>
<?php
/* PREVIOUS PAGE NAVIGATION TOP OF PAGE */
// Display Previous Page link if applicable.
if($pageNum > 1)
{
$prevPageLink = "?pageNum=".($pageNum - 1);
echo "<a href='$prevPageLink'>Previous Page</a> ";
}
/*DISPLAYING LINKS TO PAGES TOP OF PAGE*/
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
/* NEXT PAGE NAVIGATION TOP OF PAGE */
// Display Next Page link if applicable.
if($pageNum < $numOfPages)
{
$nextPageLink = "?pageNum=".($pageNum + 1);
echo " <a href='$nextPageLink'>Next Page</a>";
}
?>
</form>
<?php
/* Close the connection. */
sqlsrv_close( $conn);
?>
The change is actually not too crazy - you have most of it. Your algorithm needs to change just a little bit for the display of the pages - so instead of running your loop like you have:
/*DISPLAYING LINKS TO PAGES TOP OF PAGE*/
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("$i ");
}
You need to start at the current page number that you are on...
/*first check to make sure the number of pages don't exceed maximum*/
$totalPagesToLoop = $pageNum + $numOfPages;
if($totalPagesToLoop > ceiling(total number of pages required to show all the records ([number of total rows]/[number of rows to show per page])
{
$totalPagesToLoop = ceiling(total number of pages required to show all the records ([number of total rows]/[number of rows to show per page]);
}
/*DISPLAYING LINKS TO PAGES TOP OF PAGE*/
for($i = $pageNum; $i<=$totalPagesToLoop; $i++)
{
$pageLink = "?pageNum=$i";
print("$i ");
}
But - thats not all, you will now have to code the special 'previous' and 'next' buttons (you decide how many to increment when you click on that - google increments by 1, until it reaches 20, and then just shows 20 at a time, moving one by one. You'll have to test your variable to decide whether to show the 'previous' or 'next' button at all...
I managed to resolve the issue by using the below code:
for($i = $pageNum; $i<=$numOfPages&&$pagesadded<=9; $i++)
{
$pagesadded+=1;
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
It displays 10 pages at a time.
Cheers,
Neil