It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a table that will eventually get a row missing through deletion of records.
How could I check for that missing row in that table then fill this missing row in PHP.
I guess its using looping however I just cant work out the correct loop to use
Cheers
One way to find a missing item in a sequence is to compare count(*) and max(id).
It should give you an idea of how many are missing, then start including ranges that your checking as in a binary search.
Alternate way is just iterate over the rows ordered by id, and trigger your insert when the sequence jumps.
$lastId = 0;
foreach ($rows as $row)
{
if ($lastId + 1 != $row['id']) {
fillInRows($lastId + 1, $row['id'] -1);
}
$lastId = $row['id'];
}
function fillInRows($min, $max)
{
for ($i = $min; $i <= $max; $i++) {
// exercise left for the reader
}
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
here an array
$item = new rssGenerator_item();
$query_rss = mysql_query("SELECT * FROM `trades` WHERE `app` = '1' ORDER BY `id` DESC LIMIT 10 ")OR die(mysql_error());
while($row_rss = mysql_fetch_array($query_rss)){
$item->title = $row_rss['unit'];
$item->description = excerpt($row_rss['message'],30);
$item->link = 'http://injaa.com/'.$row_rss['link'];
$item->guid = $row_rss['site'];
$item->pubDate = RelativeTime($row_rss['tarikh']);
$rss_channel->items[] = $item;
}
$rss_feed = new rssGenerator_rss();
$rss_feed->encoding = 'UTF-8';
$rss_feed->version = '2.0';
header('Content-Type: text/xml');
echo $rss_feed->createFeed($rss_channel);
at this line
$rss_channel->items[] = $item;
all value before this clear and replace last value into array!!
i dont now where is the code is wrong?
Use
array_push($rss_channel->items, $item);
At the top(before while) add
$rss_channel->items = array();
Although #Mr Srinivas answer is better, personally I'd rewrite it like this:
$items = array();
while($row_rss = mysqli_fetch_array($query_rss)){
$items[] = $item;
}
$rss_channel->items = $items;
Admittedly I don't have the knowledge to explain why this is better, this is just how it would make sense to me.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
friend wrote a little script to get the product id and product quantity of the selected item.
here it is:
$product_id_string = $_POST['custom'];
$product_id_string = rtrim($product_id_string, ",");
$id_values = array();
$id_str_array = explode(",", $product_id_string);
$fullAmount = 0;
foreach ($id_str_array as $key => $value) {
$id_quantity_pair = explode("-", $value);
$product_id = $id_quantity_pair[0]; // product ID
$product_quantity = $id_quantity_pair[1]; // product quantity
}
how i understood it...the product id and its quantity is read just out of the custom variable?
greetings!
Exactly. The custom variable is a string "aaa,bbb,ccc,ddd". Explode converts this into an array (in my example 4 elements.) foreach() iterates them, so that everything inside it will be executed 4 times, where $value takes the values aaa, bbb and so on.
$_POST['custom'] needs to be id-quantity,id-quantity,id-quantity, e.g.
<input type="hidden" name="custom" value="A123-4,B456-1">
That would mean you bought 4 times the item A123 and once the item B456.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a csv file with 65000 records which i need to import into my database.
I have tried following code but real slow.
Is they anyway I can do this faster.
#set_time_limit(1200);
$file = './csvFiles/aw_Products.csv';
$handle = fopen( $file , 'r');
while (($row = fgetcsv($handle)) !== false) {
if( is_integer($row[0]) || $row[0] != 0 )
{
$product = new Product();
$product->merchant_id = $row[0];
$product->merc_product_id = $row[1];
$product->product_id = $row[2];
$product->product_name = $row[3];
$product->product_desc = htmlentities($row[4]);
//$product->keywords = htmlentities($row[6]);
$product->category_id = $row[5];
$product->link_url = $row[6];
$product->image_url = $row[7];
$product->price = $row[8];
$product->delivery_cost = $row[9];
//$product->deliveryAvailable = $row[12];
//$product->deliveryDetails = $row[13];
//$product->valid_to = $row[14];
//$product->valid_from = ($row[3] == 'yes') ? 1 : 0;
if( Product::find_by_id( $row[0] ) )
$product->updateRecord();
else
$product->createRecord();
}
sleep (1);
}
fclose($handle);
sleep() is probably the culprit. But I am also wondering about this: Product::find_by_id() because it might be doing a SELECT query for every INSERT. You might think about making a single SELECT query to get all of the existing DB keys into a PHP array, then you can use *in_array()* to check whether to UPDATE or INSERT. Probably goes without saying, but you will want to add to the PHP array if you INSERT.
Give up sleep() and use prepared statements for mysql insert/updates.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Let's say i have this function but i don't know how to call it...
echo getYoutubeSearchVideosFeeds($variables);
with what i should replace $variables to call $q, $orderby, $startIndex and so on?
function getYoutubeSearchVideosFeeds($criteria) {
$url = 'http://gdata.youtube.com/feeds/api/videos?';
$q = urlencode($criteria['q']);
$orderby = $criteria['orderby']; // relevance, published, viewCount, rating
$startIndex = $criteria['start-index'];
$maxResults = $criteria['max-results'];
$author = $criteria['author'];
$format = $criteria['format'];
$lr = $criteria['lr']; // fr, en
$safeSearch = $criteria['safeSearch']; //none, moderate, strict
// more code
}
Just pass it an associative array with the values listed in the function:
getYoutubeSearchVideosFeeds(array('q' => '...', 'orderby' => '...', ...));
Pass an array populated with values for each of the keys the function requires:
getYoutubeSearchVideosFeeds(array('q' => '...', 'orderby' => '...'));
getYoutubeSearchVideosFeeds(
array(
"q"=>"CriteriaValuesHere..GetItFromGDataHelp",
"start-index" => "Other Values"
.
.
.
)
);
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I search a pagination script/class/helper in PHP for an Array of data, not for SQL statements. Someone know a solution?
Off the top of my head you could have look at the LimitIterator which is part of the SPL.
If you're looking for pagination of a collection that doesn't involve SQL you may have better luck implementing pagination client side with a large collection of results. I'm just thinking that if you have a single block of results of php data then you don't really want to be reloading it again and again - usually sql would be keeping track of the next/previous results.
This jquery pagination plugin might be useful?
basic pagination you can tweak to whatever you need.
the second code snipped would be the part in your view. You could easily take this and make it more generic to be used whenever you needed it.
// !!! make sure that page is set as a url param
if( !isset( $_GET[ 'page' ] )){
$_GET[ 'page' ] = 0;
}
// find out how many rows there are total
$totalRows = mysql_query( "SELECT COUNT(*) FROM table" );
$totalRows = mysql_num_rows( $totalRows );
// find out how many pages there will be
$numPages = floor( $totalRows / $perPage );
$perPage = 15;
$offset = floor( $_GET[ 'page' ] * $perPage );
// get just the data you need for the page you are on
$pageData = mysql_query( "SELECT * FROM table LIMIT $perPage OFFSET $offset" );
then on the page to create the links
// get the current url to replace
for ($i=0; $i <= $numPages; $i++) {
if( $_GET[ 'page'] != $i ){
echo 'page '.( $i+1 ).'';
}else{
echo "page " . ( $i+1 );
}
}