This page is taking too long to load - php

I have created a view page on php zend framework. View page has 10 methods. Each method has 3 sql queries returning thee values. I am calling these 10 methods 10 times. All queries is working on the table of 24,000 rows. It's taking 2-3 minutes to load the view page. I am storing the function results in arrays and using the array values to display.
How can I implement output caching on this?
Source code for my view part is:
Those are my variables:
$no_of_customers = array();
$no_of_customers_EE = array();
$no_of_customers_NEE = array();
$unique_customers = array();
$unique_customers_EE = array();
$unique_customers_NEE = array();
$no_of_sent = array();
$no_of_sent_EE = array();
$no_of_sent_NEE = array();
$no_of_opened = array();
$no_of_opened_EE = array();
$no_of_opened_NEE = array();
$no_of_surveys = array();
$no_of_surveys_EE = array();
$no_of_surveys_NEE = array();
$promoter = array();
$promoter_EE = array();
$promoter_NEE = array();
$detractor = array();
$detractor_EE = array();
$detractor_NEE = array();
$passive = array();
$passive_EE = array();
$passive_NEE = array();
one of the function i am using is
function unique_customer($con,$start_date,$end_date ,&$unique_customers,$i,$customerTable,&$unique_customers_EE,&$unique_customers_NEE)
{
$result = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' ;");
$rs = mysqli_fetch_array($result);
$unique_customers[$i] = $rs['Total'];
unset($rs);
$result_EE = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total_EE, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' AND product = '';");
$rs_EE = mysqli_fetch_array($result_EE);
$unique_customers_EE[$i] = $rs_EE['Total_EE'];
unset($rs_EE);
$result_NEE = mysqli_query($con,"SELECT COUNT(DISTINCT emailAddress) AS Total_NEE, product FROM $customerTable WHERE importDate >='$start_date' AND importDate <'$end_date' AND product != '';");
$rs_NEE = mysqli_fetch_array($result_NEE);
$unique_customers_NEE[$i] = $rs_NEE['Total_NEE'];
unset($rs_NEE);
}
Calling these functions 10 times and storing the values in arrays

Related

PHP - mysqli_fetch_assoc, 2 results then into an array

$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` =' $caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$selectganadores3 = $selectganadores2['usuario'];
array_push($arrayresultados,$selectganadores3);
}
Why the results are not pushing into the array? I'm new with Programming, sorry for my errors.
Try it with $arrayresultados[]
$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` ='$caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$arrayresultados[] = $selectganadores2['usuario'];
}

Updating all columns that has same title but have different ids

I have a table named collection and has columns like title,author,edition,volume, etc. Now, i want to be able to update all columns that has been edited with the same title but have different ids.
here is my example code
$con=mysqli_connect("localhost","root","","library03");
$check="SELECT * FROM collection WHERE title = '$_POST[title]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
for($i=0;$i<=COUNT($data);$i++){
$sql="UPDATE collection SET author = '$new_author' WHERE title = '$_POST[title]'";
$sql_connect = mysqli_query($sql);
And these are under my $_POST
$collection = Collection::find_by_id($_POST['id']);
$collection->date_received = $_POST['date_received'];
$collection->title = $_POST['title'];
$collection->author = $_POST['author'];
$collection->edition = $_POST['edition'];
$collection->volume = $_POST['volume'];
$collection->pages = $_POST['pages'];
$collection->cost = $_POST['cost'];
$collection->publisher = $_POST['publisher'];
$collection->pub_year = $_POST['pub_year'];
$collection->remarks = $_POST['remarks'];
$collection->call_number = $_POST['call_number'];
$collection->col_copy = $_POST['col_copy'];

Mysql display all records

I need an SQL query that displays all the records if its duplicated also. For instance say
select * from table where true and p_id in(1,2,1,1)
displays only records from 1 and 2 but i need it to be repeated when given in while loop.
Update with code:
$cook = unserialize($_COOKIE["pro_cook"]);
foreach ($cook as $something) {
$merc[] = $something;
}
foreach ($size as $new_size) {
$size_array[] = $new_size;
}
$items = count($merc);
$mer = rtrim(implode(',', array_reverse($merc)), ',');
$fulclr = "and p_id in (".$mer.")";
$asd = "(p_id,".$mer.")";
$result = mysql_query("select * from product_details where true ".$fulclr." order by field".$asd."");
Hope this will help
$ids = "1,2,1,1";
$sql = "select * from table where true and p_id in (".$ids.")";
$rec = mysql_query($sql);
$dbData = array();
while($res = mysql_fetch_assoc($rec)) {
$dbData[$res['p_id']] = $res;
}
$ids = explode(',', $ids);
$newArray = array();
foreach ($ids as $id) {
if (!empty($dbData[$id])) {
$newArray[] = $dbData[$id];
}
}

display array of items from DB php/MySQL

I am unsure how to display the items field. I want to display two tables of data; one that has all the items from a user and one with all the items to teh user. All I've been able to output is the item_id's(I pasted the html below). How to get all the item info from these ids, which is in the item table, and populate the HTML?
trans table
item table
$from = 1;
$sql = $db->prepare("SELECT * FROM test WHERE from_id = :id");
$sql->bindValue(':id', $from);
$sql->execute();
while($row = $sql->fetch())
{
$t =$row['items'];
$u =$row['to_id'];
$trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$t</td>
<td>$u</td></tr>";
}
HTML DISPLAY
Try this!
<?php
$from = 1;
$sql = $db->prepare("SELECT * FROM test WHERE from_id = :id");
$sql->bindValue(':id', $from);
$sql->execute();
while($row = $sql->fetch())
{
$t =$row['items'];
$u =$row['to_id'];
$itemIDs = #explode(",", $t);
$items = array();
foreach($itemIDs as $ID){
$sqlItem = $db->prepare("SELECT itemname FROM itemtable WHERE itemid = :itemid");
$sqlItem->bindValue(':itemid', $ID);
$sqlItem->execute();
$itemname ='';
while($rowItems = $sqlItem->fetch())
{
$itemname .=$rowItems['itemname'];
}
$items[$t] = $itemname;
}
$trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$items[$t]</td> <td>$u</td></tr>";
}
below is my code for testing,
<?php
$from = 1;
$sql = mysqli_query($db,"SELECT * FROM test WHERE from_id = '$from'");
while($row = mysqli_fetch_array($sql))
{
$t =$row['items'];
$u =$row['to_id'];
$itemIDs = #explode(",", $t);
$itemname ='';
foreach($itemIDs as $ID){
$sqlItem = mysqli_query($db, "SELECT itemname FROM itemtable WHERE item_id = '$ID'");
while($rowItems = mysqli_fetch_array($sqlItem))
{
$itemname .= $rowItems['itemname'].', ';
}
$items[$u] = $itemname;
}
$trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$items[$u]</td> <td>$u</td></tr>";
}
echo "<table>".$trans."</table>";
?>
Note : change my queries with ur need
in ur while loop
while($row = $sql->fetch())
{
$items_array = array();
$items_array = explode(",",$row["items"]);
foreach($items_array as $key => $value)
{
//modify ur query according to ur need
$query3 = "SELECT item_name
FROM item_table
WHERE item_id =".$value." ";
$result3 = mysql_query($query3);
$row3 = mysql_fetch_assoc($result3);
$item_name .= $row3['subcategory_name'].", ";
}
}
now ur array will contains item_id,
use foreach loop in ur while loop and get info of Item from item table with item_id from expolode function
Within while you will have to fire new query that will get the information of items.
For eg :
"SELECT * FROM item_info_table WHERE id IN (id1,id2, id3)"
It will return you the item information corresponding to the id's.
The data is not normalized. Get it to normalize and you'll have a much better and cleaner solution.

Why is the query returning only one set of data?

The problem I have is when I echo or print the following variables, the data I receive is that of the last business listed in my table only.
At present no matter the listing I click I get the same set of data for the last business returned.
As you can see in the below code I am passing the business_name from the clicked listing to be used in my query to find the relevant business profile information.
$business_name = mysql_real_escape_string($_GET['business_name']);
$query = "SELECT
business_id,
category,
years_recommended,
profile_size,
business_name,
established,
employees,
service,
strengths,
ideal_for,
reassurance
FROM
business_data
WHERE
business_name = '$business_name'
AND
profile_size = 'A'
OR
profile_size = 'B'
OR
profile_size = 'C'
OR
profile_size = 'D'
OR
profile_size = 'E'";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
}
echo...
If you need more information please let me know.
Is there anything wrong with my code?
Many thanks in advance.
Your echo call is outside the fetch loop, so you'll only see the last result even though the others were returned.
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
// Echo **inside** the loop
echo...
}
If you wish, you can store all the results in a large array, which can then be used anywhere subsequently in your script, as many times as needed:
// Array for all results
$results = array();
while($row = mysql_fetch_array($result)) {
// Append each row fetched onto the big array
$results[] = $row;
}
// Now use it as needed:
foreach ($results as $r) {
echo $r['profile_size'];
print_r($r);
}
your echo should be inside the loop

Categories