PHP Count the number of times a value appears in result

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

PHP Count the number of times a value appears in result



I'm selecting data from a database:


$status = "Attended";
$query = $conn->prepare('SELECT * FROM jobstocomplete WHERE applicant_mid=:mID AND status=:status');

$query->execute(array(':mID' => $mID, ':status' => $status));



I then put the results into a table using a while loop such as below:


if ($query->rowCount() > 0)

echo "<div class='panel-body table-responsive'>";
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th id=''>Job ID</th>";
echo "<th id=''>Total</th>";
echo "<th id=''>Company Name</th>";
echo "<th id=''>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";

while ($row = $query->fetch(PDO::FETCH_ASSOC))

$companyname = $row['compname'];

echo "<tr>";
echo "<td>" . htmlspecialchars($row['jobid'], ENT_QUOTES, 'utf-8') . "</td>";
echo "<td>" . htmlspecialchars($row['total'], ENT_QUOTES, 'utf-8') . "</td>";
echo "<td>" . htmlspecialchars($companyname, ENT_QUOTES, 'utf-8') . "</td>";

$completed = $row['compl'];

if (empty($completed))
echo "<td>" . "<a href='#' data-toggle='tooltip' data-placement='top' title='Not yet complete' disabled>Mark as completed</button></a>" . "</td>";
else
echo "<td>" . "<a class='btn custombtn' href='#invoiceModal' data-toggle='modal' data-jid='".htmlspecialchars($row["jobid"], ENT_QUOTES, 'utf-8')."' data-mid='".htmlspecialchars($row["applicant_mid"], ENT_QUOTES, 'utf-8')."' data-upid='".htmlspecialchars($row["up_id"], ENT_QUOTES, 'utf-8')."'> Mark as completed</a>" . "</td>";


echo "</tr>";



else
echo "<p>Looks like you've not completed any jobs</p>";



When my users click "Mark as Complete" this then launches a bootstrap modal which then triggers jQuery/ajax to generate an invoice using fpdf.



What I am wanting to achieve is calculate the number of times a company name appears. For example if among the results $companyname = "Heaven Autos" appears more than once I want my code to launch a different modal which will alert



"Do you want to generate a multiple invoice as you have carried out more than one job for this company"



This will then trigger a different php files using jQuery/ajax and calculate total etc and generate an invoice based on that.



I invisaged doing this running a query and a simple if statement but didn't know how to start the logic.



I hope this makes sense and I hope you can help.



p.s I have already tried things like array_count etc and haven't had much luck, any advice appreciated.





Have you considered adding an ID data attribute and in your modal trigger loop through all the IDs and if length > 1 trigger the second modal? You basically need an identifier either data attribute or unique class for each entry that you can check before your trigger with this kind of logic
– Ilan P
2 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Hystrix command on request collapser fallback