I've started using PHPExcel, and I have to say, it's a wonderful piece of software. However, I can't get pass through this piece of code that is below written, and I don't know how to act.
<?php
$query1 = "SELECT DISTINCT id FROM table_name;";
$result1 = mysql_query($query1);
$registers1 = mysql_fetch_object($result1);
$row = 15;
$row2 = 16;
$row3 = 17;
$row4 = 18;
$col = 1; // B
for ($i = 1; $i <= $registers1; $i++) {
while ($row_x = mysql_fetch_object($result1)) {
$startColumn = $col;
$endColumn = $col+5;
$range = PHPExcel_Cell::stringFromColumnIndex($startColumn) . $row.':' .
PHPExcel_Cell::stringFromColumnIndex($endColumn) . $row2;
$objPHPExcel->getActiveSheet()->mergeCells($range);
// Print rest of cells.
$query2 = "SELECT * FROM table2_name WHERE id = '".$registers1->id."' ORDER BY ID;";
$result2 = mysql_query($query2);
while ($row_y = mysql_fetch_object($result2)) {
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row4, $row_y->value_xyz);
// Print rest of cells.
$row4++;
}
$row += 20;
$row2 += 20;
$row3 += 20;
}
}
?>
Now, as you can see, there are two different querys on that code. I want to dump these results onto an XLS page (that is already created with PHPExcel routines) that will show:
id
Values for that id
(spaces)
id
Values for that id
and so on. I know that this question might be something odd to ask (aka easy to solve), but I can't stare at this code any more. Any ideas? Thanks all.
Finally, I've come across the solution.
Looks like I was doubling the loops without needing to. Once that this was solved, the rest was iterating the values and deleting rows instead of adding those.
Thanks anyway for all the help.
Related
I want to set the maximum number of row in each table and continue the data to the next page in the html. For example, I have 44 records and I need to show only 10 records in first html page and another 10 in another html page and goes on and finally with the last 4 records in the last page. I just need to know the logic to be used in this with for loop.
I tried this to display the first 10 records but, how to display the other records
<% for (var i = 0; i < 10; i++) { %>
I guess you need a simple pager but with data on different independent pages
Here is a part of code ... you just need to change $pageno variable to current page .. or you can catch it with GET or extract from pagename.
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$conn=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$total_pages_sql = "SELECT COUNT(*) FROM table";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($res_data)){
//here goes the data
}
mysqli_close($conn);
getChildIds can be very slow if running it against a large set of resources, so I am trying to write a query and some code to get all the child ids faster.
However I am getting different results from getChildIds & my script.
Can anyone see why these would be yielding different results?
Method using getChildIDs:
$parentDepth = isset($scriptProperties['parentDepth']) ? $scriptProperties['parentDepth'] : 5;
$parents = explode(',', $parents);
$children = array();
foreach ($parents as $parent){
$ids = $modx->getChildIds($parent,10,array('context' => 'web'));
foreach($ids as $id){
$children[] = $id;
}
}
echo ' number of children = ' . count($children);
method using queriees & a loop:
$comma_separated = implode(",", $parents);
$sql = "SELECT `id` from modx_site_content where `parent` IN (".$comma_separated.") and published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";
$results = $modx->query($sql);
$mychildren = array();
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$mychildren[] = $row['id'];
}
for($i=0; $i <= 10; $i++){
$comma_separated = implode(",", $mychildren);
$sql = "SELECT `id` from modx_site_content where `parent` IN (".$comma_separated.") and published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";
$results = $modx->query($sql);
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$mychildren[] = $row['id'];
}
}
echo ' number of children = ' . count($mychildren);
The getChildIDs method takes nearly 1.5 seconds to run and gives about 1100 results
The SQL/script method runs un under 0.1 second and gives 1700 results.
Either I'm not appending the child ids to the array properly ~or~ maybe getChildIDs is filtering out some other results?
does anyone have any clues as to what could be happening here?
You can try to use built-in method of pdoFetch.
$pdo = $modx->getService('pdoFetch');
$ids = $pdo->getChildIds('modResource', 0);
print_r($ids);
It also recursive, but can be better in some situations.
Of course, you need to install pdoTools from the repository first.
Looking at the code again, the discrepancy in results is pretty obvious now. I'm appending results to the child id array, it's inserting duplicates since one parent can have many children.
the solution to avoid duplicates:
$mychildren[$row['id']] = $row['id'];
getChildIds - is recursive, so it slower by default.
I'm trying to convert a part of code into prepare statements and can't make work the select statement.
It doesn't throw an error, it just don't list the items. Instead of displaying the results it says there are no entries (double check and there are in db).
Any help is much appreciated.
if($_GET[action]=='view-rss')
{
$count =0;
$stmt = $GLOBALS["___mysqli_ston"]->prepare("select * from rss where lang=(?)");
$stmt->bind_param('s', $_SESSION[session_lang]);
$stmt->execute();
$stmt->close();
while($stmt=mysqli_fetch_assoc($query))
{
$rss_ids[] = $row['id'];
$rss_urls[] = $row['url'];
$rss_titles[] = $row['title'];
$rss_publishes[] = $row['published'];
$count= $count +1;
}
$smarty->assign("rss_ids", $rss_ids);
$smarty->assign("rss_urls", $rss_urls);
$smarty->assign("rss_titles", $rss_titles);
$smarty->assign("rss_publishes", $rss_publishes);
$smarty->assign("count", $count);
}
I have one database with two tables: "music" and "agenda".
But for some reason once I have queried one table, I cannot perform a similar query on the other table. Or in any case, its variables are empty.
I'd think I could just keep the connection open and perform a second query after the first "while". Like so:
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('erikverwey');
$result = mysql_query("SELECT * FROM agenda ORDER BY date DESC LIMIT 0, 2") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$count++;
$date[$count] = $row['date'];
$time[$count] = $row['time'];
$place[$count] = $row['place'];
$venue[$count] = $row['venue'];
$who[$count] = $row['who'];
$concert[$count] = $row['concert'];
$urlvenue[$count] = $row['urlvenue'];
}
$result2 = mysql_query("SELECT * FROM music ORDER BY id LIMIT 0, 5") or die(mysql_error());
while($row = mysql_fetch_array($result2)) {
$count++;
$song[$count] = $row['song'];
$artist[$count] = $row['artist'];
$duration[$count] = $row['duration'];
$url[$count] = $row['url'];
}
mysql_close();
?>
But no. In this case, all the variables from the table "music" remain empty.
I've been looking for an answer, but no luck. I'm still new to MySQL, though, so apologies beforehand if this is standard stuff. Thanks!
I found the glitch. Because the counter "$count" was used a second time, it started where it left off and couldn't find any data.
Use a different counter, also in the variables (!), and all is good.
I have this script
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But in the .xls file it prints only one user. Why id doesnt print all of the users ? W
Thanks in advance.
I make the change you said with $sheet
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$sheet->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But it still prints out only one record. And yes when i run the query in phpmyadmin it returns more than one record.
How can i print out data from mySql table.. What is going wrong ?
I am pretty sure it is because you are using a unique identifier (WHERE tmima_id='6'). It is only finding the results for that one unique identifier and displaying that. Hope this helps.
$i is being reset to row 3 every loop. Set $i=3; before the while loop, not inside it.