Jump to content
Sign in to follow this  
shmideo

How to count records from two columns

Recommended Posts

Hi all

 

PHP newbie and first post!

 

I hope someone can help. I have looked at some of the tutorials but still can't get my head around how to get the data from within a PHP script.

 

Trying to create a form with two fields "Date" and "Channel" to look at the 'calldate' and 'channel' columns and then give a count result, so just want to find whatever specified on the form and give the number of ocurrences found grouped by date.

 

Ie, how many ocurrences for specified 'chanel' %CLI/1234567% for a given date.

 

Thanks

 

 

 

Share this post


Link to post
Share on other sites

Where is your data that you're trying to search through? Is it in a database? What code do you have so far?

Share this post


Link to post
Share on other sites

Well, break it up into pieces. One piece is to get the data from the form, another one is to connect to and get data from the database, then you can combine them.

Share this post


Link to post
Share on other sites

This is what I have so far, but stuck on the date part.

 

<?php//// CONNECT TO THE DATABASE SERVER (use your credentials)//$db = new mysqli(localhost,root,"",jm_db);//// GET THE VALUE TO SEARCH FROM FROM SUBMITTED FORM DATA//$search = isset($_GET['search']) ? $_GET['search'] : '';//// ADD THE WILDCARD CHARACTERS//$sqlSearch = '%'.$search.'%';//// PREPARE AND EXECUTE THE QUERY//$sql = "SELECT calldate, channel , COUNT(*) as total FROM asterisk_cdr WHERE tablefield LIKE '%given string%' GROUP BY calldate"; $stmt = $db->prepare($sql);$stmt->bind_param('s', $sqlSearch);$stmt->execute();$res = $stmt->get_result();//// LOOP THROUGH THE RESULTS AND BUILD THE HTML OUTPUT//$output = "";while ($row = $res->fetch_row()) { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>n";}?><!DOCTYPE HTML ><html lang="en"><head> <title>Sample</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" > <style type='text/css'> table { border-collapse: collapse; } th { background-color: #369; color: white; } td { background-color: #eee; } </style></head><body><form> Call Date: <input type="text" name="search" value="<?=$search?>" size="10"> <input type="submit" name="btnSubmit" value="Search"></form><form> Date Ie, 2014/05/19: <input type="text" name="search" value="<?=$search?>" size="10"> <input type="submit" name="btnSubmit" value="Search"></form><hr/><table border='1'> <tr><th>Table Field</th><th>Total</th></tr> <?=$output?></table><p> </p></body></html>

Share this post


Link to post
Share on other sites

This is what I have so far, but stuck on the date part.

What do you mean? What's the date part?You have an issue with your query. You're trying to bind a parameter, but there isn't a placeholder for it in the query string. When you're using prepared statements you need to have placeholders in the query for each value. You're not doing any error checking either, you should check for errors when you prepare and then execute the query. Each class has properties to check if an error occurred, you would check the statement class after calling execute, for example:http://php.net/manual/en/mysqli-stmt.error.phpIn general, it's not the best idea to copy and paste code. It doesn't help you understand how the code works. You have a prepared statement there, for example, but do you understand how it works and why you should use it?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...