By using GROUP_CONCAT
we group the ID
s and CPC
s separated by a comma for later use and with GROUP BY
the user ID
we will get a single row of result per user.
On the foreach
we deduce each CPC
from the MONETOS
and from there we set who needs to be disabled to the $to_disable
array that is later used to disable all the id's needed to.
$query = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc,
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$to_disable = array();
$to_enable = array();
foreach($rows = $connector->fetchArray($query) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>
";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>
";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>
";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>
";
$to_enable[] = $content_id;
}
}
echo "<br>
";
}
if (sizeof($to_disable) > 0)
{
$connector->query("UPDATE content
SET active = 0
WHERE id IN (".implode(',',$to_disable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_disable).")<br>
";
if (sizeof($to_enable) > 0)
{
$connector->query("UPDATE content
SET active = 1
WHERE id IN (".implode(',',$to_enable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_enable).")";
Using your SQL dump this is what I get:
USER 9 currently have 15!
USER 9 after CONTENT 16 now have 10!
USER 9 after CONTENT 30 now have 5!
USER 9 after CONTENT 17 now have 4!
USER 9 after CONTENT 31 now have -1!
USER 9 should have the CONTENT 31 disabled!
USER 9 after CONTENT 18 now have -4!
USER 9 should have the CONTENT 18 disabled!
USER 9 after CONTENT 32 now have -9!
USER 9 should have the CONTENT 32 disabled!
USER 9 after CONTENT 20 now have -13!
USER 9 should have the CONTENT 20 disabled!
USER 9 after CONTENT 33 now have -18!
USER 9 should have the CONTENT 33 disabled!
USER 9 after CONTENT 21 now have -22!
USER 9 should have the CONTENT 21 disabled!
USER 9 after CONTENT 34 now have -26!
USER 9 should have the CONTENT 34 disabled!
USER 9 after CONTENT 22 now have -31!
USER 9 should have the CONTENT 22 disabled!
USER 9 after CONTENT 24 now have -36!
USER 9 should have the CONTENT 24 disabled!
USER 9 after CONTENT 26 now have -41!
USER 9 should have the CONTENT 26 disabled!
USER 9 after CONTENT 29 now have -45!
USER 9 should have the CONTENT 29 disabled!
USER 10 after CONTENT 28 now have 95!
USER 11 after CONTENT 27 now have -4!
USER 11 should have the CONTENT 27 disabled!
And the UPDATE
result:
UPDATE content SET active = 0 WHERE id IN (31,18,32,20,33,21,34,22,24,26,29,27)
And here is the sample code used to read the data as is:
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$result = $con->prepare($sql);
$result->execute();
if ($result->rowCount() == 0)
{
die('No data found...');
}
$to_disable = array();
$to_enable = array();
foreach($result->fetchALL(PDO::FETCH_ASSOC) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>
";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>
";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>
";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>
";
$to_enable[] = $content_id;
}
}
echo "<br>
";
}
if (sizeof($to_disable) > 0)
{
$ids = implode(',',$to_disable);
$sql = "UPDATE content
SET active = 0
WHERE id IN ({$ids})";
$disable = $con->prepare($sql);
$disable->execute();
echo "UPDATE content SET active = 0 WHERE id IN ({$ids})<br>
";
}
else
{
echo "Nothing was disabled...<br>
";
}
if (sizeof($to_enable) > 0)
{
$ids = implode(',',$to_enable);
$sql = "UPDATE content
SET active = 1
WHERE id IN ({$ids})";
$enable = $con->prepare($sql);
$enable->execute();
echo "UPDATE content SET active = 1 WHERE id IN ({$ids})";
}
else
{
echo "Nothing was enabled...";
}
$con = NULL;