Jump to content

Inserting routine into a simple script without upsetting the array


promotec
Go to solution Solved by ginerjm,

Recommended Posts

Hi Guys I have been working on a script for my website, the script itself works fine and does what it is meant to

I have a free tarot reading request page, the visitor enters name-email-gender- and a question into the form and submits it, this data is written to MySQL.

When I call the script below, it reads from MySQL creates a array of any free reading requests and displays them in a table. I then click on delete or answer and access those functions in other scripts, this all works great.

heres a screen capture of the output.

 

cap.jpg

 

I have created another column called readings
Im trying to inset a routine that queries MySQL
and counts how many readings that visitor has had
including the current reading request. based on how many
records have the visitors email address, heres the code Im trying to insert into the script.

<!-- Start of revisit count code-->

<?php
$times = "$row['name']";
{
$query = "SELECT email, COUNT(email) FROM contacts WHERE Name='$times'"; 
	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
	echo "". $row['COUNT(email)'] ."";
	
}}
?>

<!-- End of revisit count code -->

The code as a standalone script works, but when entered into my script it stops the array output after the first row/record, I have tried including it as a iframe, clalling the file with a query string to pass the email variable to that script, struck problems , I have tried as an include but it wouldn't work calling the file name with a query string in the include statement.

 

Here is my script

<?php
require_once('cfg.php');  

$sql = "SELECT * FROM contacts WHERE answer IS NULL ORDER BY id ASC"; 
$result = mysql_query($sql) or die(mysql_error());
?>

<html>
<head>
	<title>Free Reading Requests</title>
</head>  
<body>

<h3><p align="center">Free Reading Admin.</p></h2>
<p></p>
      <div align="center"><form action="list.php" method="post">
      <input type="submit" value="View Answered Questions." />
      </form></div>
<p></p>

<?php while($row = mysql_fetch_array($result)): ?>

<table align="center" cellspacing="2" cellpadding="2" border="1">
<tr>
<td>
<table border=0 width=1450 style='table-layout:fixed' >
 
 <col width=50>
 <col width=200>
 <col width=225>
 <col width=75>
 <col width=500>
 <col width=100>
 <col width=150>
 <col width=100>
 <col width=100>
 
 <tr bgcolor="#C4F868">
    <td>ID</td>
	<td>Name</td>
	<td>Email</td>
	<td>Gender</td>
	<td align="center">Question</td>
	<td align="center">Readings</td>
	<td>Date/Time</td>
	<td align="center">Delete</td>
	<td align="center">Answer</td>
  </tr>
<tr bgcolor="#F3F4A4">
    <td valign="top"><?php echo $row['id']; ?></td>
	<td valign="top"><?php echo $row['name']; ?></td>
	<td valign="top"><?php echo $row['email']; ?></td>
	<td valign="top"><?php echo $row['gender']; ?></td>
	<td valign="top"><?php echo $row['question']; ?></td>
	<td valign="top">
	
<!-- Start of revisit count code-->

<?php

$times = "$row['name']";

{

$query = "SELECT email, COUNT(email) FROM contacts WHERE Name='$times'"; 
	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
	echo "". $row['COUNT(email)'] ."";
	
}}
?>

<!-- End of revisit count code -->

	</td>
	<td valign="top"><?php echo $row['date']; ?></td>
    
    <td align="center" valign="top">
      <form action="delete.php" method="post">
        <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
        <input type="submit" value="Delete" />
      </form>
    </td>
	   <td align="center" valign="top">
      <form action="answer.php?id=<?php echo $row['id']; ?>" method="post">
      <input type="submit" value="Answer" />
      </form>
    </td>
    </tr>
   <tr>
  <td align="center" colspan="7"></td>
  </tr>
  </table>
</tr>
</table>
</p>


<?php endwhile; ?>
</body>
</html>

Inserting a routine in the middle of the array output appears problematic is there any way around this problem, I tried inserting the code further up the script and just using a variable in the TD but that didn't work. as I think the main routine has to be below the array routine...
My head hurts :-)

 

Heres some feedback off another forum, a guy did answer then wouldn't elaborate any further

this stops your code dead

$times = "$row['name']";

You don't need it nor do you need to get a second query. Add the 

SELECT email, COUNT(email).....

to the original query, at the top of the page, and print this column as you do all others.

Use 

COUNT(email) AS number_email

or something like that. 

So my question is this

how do I include            SELECT email, COUNT(email)

as part of this query

<?php
require_once('cfg.php');  

$sql = "SELECT * FROM contacts WHERE answer IS NULL ORDER BY id ASC"; 
$result = mysql_query($sql) or die(mysql_error());
?>

so that the count email function becomes part of the array then prints for each record in the table.

 

and also is this the correct way to echo the email count  for each record in the table.

<td valign="top"><?php echo $row['number_email']; ?></td>

Sorry for being so long winded guys, but any assistance would be greatly appreciated, it got me stumped, and while a simple addition to the original script, its worthwhile for me to accomplish.

Kind regards Chris

Link to comment
Share on other sites

Hi Gristoi  thankyou for your input I think you are correct however, I think the way I was going about things was wrong, I think I just need to add the COUNT (email) needs adding to the top query on my script then  have that echo the result in my table below that.

Im just not sure of the correct syntax to do so I played around for a day or two and didn't succeed :-(

Link to comment
Share on other sites

  • Solution

change your select * from email as was suggested to:

 

select *,count(email) as num_emails from email.

 

Now you have an additional field in your query results to be displayed in your table.

 

The reason it was failing was because you were re-using the $results var in your count query thus destroying the original query results. If you had done that 2nd query using a diff var you would have been alright, although inefficient

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.