Jump to content

Recommended Posts

Hi,

I recently started using PHP and have a bitter/ sweet relationship with it.

 

I have a database that supplies the contents to my website using echo. Other contents will load fine except for echos. Instead of giving me the value it shows the entire code.

 

Here is the link to the page I am working on:

http://customercenter.rlaartistry.com/Survey/index.php?pid=2

 

If more info is need I will gladly supply. I am a novice so please make you answer simple.

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/267793-echo-php-variable-from-database/
Share on other sites

You should give us the relevant code.

Sorry about that, here is the code, it is a survey form and I want it to remember the value of the radio buttons. I also want after the form is submitted base on the value of question 3 to display a different exit message. Basically is the customer is satisfied they get a "thank you" and if the customer is dissatisfied they get a "sorry" on the thank you page.

 

Here is the code for the "thank you" page:

 

if($ans3 <= 2){
header ("location: http://customercenter.rlaartistry.com/Survey/index.php?pid=3&conclusion=1");
}else{
header ("location: http://customercenter.rlaartistry.com/Survey/index.php?pid=3&conclusion=2");
}

And here is the form:

 

 

  <span class="alert"><?php echo $errorMsg; ?></span>
  <form action="" method="post" name="newSiteSurvey">
  <span class="title_lg">1.</span> How likely are you to recommend RLA Artistry to a friend or colleague?<br /><br />
  <span class="alert"><?php echo $errorMsg1; ?></span>
<table class="likelyTable" width="800" border="0">
  <tr>
    <td>Not likely at all</td>
    <td>Not Likely</td>
    <td>Neutral</td>
    <td>Likely</td>
    <td>Extremely Likely</td>
  </tr>
  <tr>
    <td width="160"><input type="radio" name="likely" value="1" id="likely_1" <?php if(isset($_POST['likely']) && $_POST['likely'] == '1'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="2" id="likely_2" <?php if(isset($_POST['likely']) && $_POST['likely'] == '2'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="3" id="likely_3" <?php if(isset($_POST['likely']) && $_POST['likely'] == '3'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="4" id="likely_4" <?php if(isset($_POST['likely']) && $_POST['likely'] == '4'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="5" id="likely_5" <?php if(isset($_POST['likely']) && $_POST['likely'] == '5'): ?>checked='checked'<?php endif; ?> /></td>
  </tr>
</table>
<hr />
    <p><span class="title_lg">2.</span> Think back to when you first visited RLAArtistry.com, were there any areas of the site that you think needed improvement? (select the most important one that applies):<br /><br />
<span class="alert"><?php echo $errorMsg2; ?></span>
    <label>
      <input type="radio" name="RLASite" value="service" id="RLASite_0" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'service'): ?>checked='checked'<?php endif; ?> />
      Finding the service that I wanted.</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="signup" id="RLASite_1" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'signup'): ?>checked='checked'<?php endif; ?> />
      Creating my account (adding my contact information and uploading logo ).</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="design" id="RLASite_2" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'design'): ?>checked='checked'<?php endif; ?> />
      Site appearance (Background, text color and site layout).</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="product" id="RLASite_3" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'product'): ?>checked='checked'<?php endif; ?> />
      Product information.</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="like" id="RLASite_4" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'like'): ?>checked='checked'<?php endif; ?> />
      Nope. I like RLA Artistry just as it is.</label>
    <br />
  </p>
<hr />
  <p><span class="title_lg">3.</span> When you received your new website from RLA Artistry, did it meet your expectations?<br /><br />
<span class="alert"><?php echo $errorMsg3; ?></span>
<label>
      <input type="radio" name="expect" value="1" id="expect_0" <?php if(isset($_POST['expect']) && $_POST['expect'] == '1'): ?>checked='checked'<?php endif; ?> />
      It was even better than I had expected.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="2" id="expect_1" <?php if(isset($_POST['expect']) && $_POST['expect'] == '2'): ?>checked='checked'<?php endif; ?>  />
      Yes, it was just as I expected.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="3" id="expect_2" <?php if(isset($_POST['expect']) && $_POST['expect'] == '3'): ?>checked='checked'<?php endif; ?>  />
      Not exactly, I expected a little bit more, could be better.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="4" id="expect_3" <?php if(isset($_POST['expect']) && $_POST['expect'] == '4'): ?>checked='checked'<?php endif; ?>  />
      The final product some what met my expectations but I am not completely satisfied.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="5" id="expect_4" <?php if(isset($_POST['expect']) && $_POST['expect'] == '5'): ?>checked='checked'<?php endif; ?>  />
      No, I was disappointed. The final product didn't meet my expectations at all.</label>
    <br />
  </p>
      <input name="Submit" type="submit" class="button1" value="Submit" />
  </form> 

 

 

PHP should not be stored within a database.

 

I might not have mentioned but I am a novice in self training with PHP. I am fluent in HTML and CSS, and do an awesome job in Photoshop, but not good at scripting. Is there an alternative to achieve the about without having the PHP in my database?

 

Thanks again

Please use the


tags around your code, as it helps make both your post and your code a lot easier to read.

Also, how to you store the form itself, and how to you include it into the page?

 

Sorry about that.

I basically copy and paste the form code into a long text field in the database, then in the index.php page I do a mysqli_query and echo the variable from it into the body. Its like a template, with 1 page and page ID determine the content and title. The same script can be seen here without the database and it works fine;

 

http://customercenter.rlaartistry.com/Survey/new_site_survey.php?url=swatballers.org

 

Not sure if I need to repost the form but here is the correct way;

 

  <span class="alert"><?php echo $errorMsg; ?></span>
  <form action="" method="post" name="newSiteSurvey">
  <span class="title_lg">1.</span> How likely are you to recommend RLA Artistry to a friend or colleague?<br /><br />
  <span class="alert"><?php echo $errorMsg1; ?></span>
<table class="likelyTable" width="800" border="0">
  <tr>
    <td>Not likely at all</td>
    <td>Not Likely</td>
    <td>Neutral</td>
    <td>Likely</td>
    <td>Extremely Likely</td>
  </tr>
  <tr>
    <td width="160"><input type="radio" name="likely" value="1" id="likely_1" <?php if(isset($_POST['likely']) && $_POST['likely'] == '1'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="2" id="likely_2" <?php if(isset($_POST['likely']) && $_POST['likely'] == '2'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="3" id="likely_3" <?php if(isset($_POST['likely']) && $_POST['likely'] == '3'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="4" id="likely_4" <?php if(isset($_POST['likely']) && $_POST['likely'] == '4'): ?>checked='checked'<?php endif; ?> /></td>
    <td><input type="radio" name="likely" value="5" id="likely_5" <?php if(isset($_POST['likely']) && $_POST['likely'] == '5'): ?>checked='checked'<?php endif; ?> /></td>
  </tr>
</table>
<hr />
    <p><span class="title_lg">2.</span> Think back to when you first visited RLAArtistry.com, were there any areas of the site that you think needed improvement? (select the most important one that applies):<br /><br />
<span class="alert"><?php echo $errorMsg2; ?></span>
    <label>
      <input type="radio" name="RLASite" value="service" id="RLASite_0" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'service'): ?>checked='checked'<?php endif; ?> />
      Finding the service that I wanted.</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="signup" id="RLASite_1" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'signup'): ?>checked='checked'<?php endif; ?> />
      Creating my account (adding my contact information and uploading logo ).</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="design" id="RLASite_2" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'design'): ?>checked='checked'<?php endif; ?> />
      Site appearance (Background, text color and site layout).</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="product" id="RLASite_3" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'product'): ?>checked='checked'<?php endif; ?> />
      Product information.</label>
    <br />
    <label>
      <input type="radio" name="RLASite" value="like" id="RLASite_4" <?php if(isset($_POST['RLASite']) && $_POST['RLASite'] == 'like'): ?>checked='checked'<?php endif; ?> />
      Nope. I like RLA Artistry just as it is.</label>
    <br />
  </p>
<hr />
  <p><span class="title_lg">3.</span> When you received your new website from RLA Artistry, did it meet your expectations?<br /><br />
<span class="alert"><?php echo $errorMsg3; ?></span>
<label>
      <input type="radio" name="expect" value="1" id="expect_0" <?php if(isset($_POST['expect']) && $_POST['expect'] == '1'): ?>checked='checked'<?php endif; ?> />
      It was even better than I had expected.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="2" id="expect_1" <?php if(isset($_POST['expect']) && $_POST['expect'] == '2'): ?>checked='checked'<?php endif; ?>  />
      Yes, it was just as I expected.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="3" id="expect_2" <?php if(isset($_POST['expect']) && $_POST['expect'] == '3'): ?>checked='checked'<?php endif; ?>  />
      Not exactly, I expected a little bit more, could be better.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="4" id="expect_3" <?php if(isset($_POST['expect']) && $_POST['expect'] == '4'): ?>checked='checked'<?php endif; ?>  />
      The final product some what met my expectations but I am not completely satisfied.</label>
    <br />
    <label>
      <input type="radio" name="expect" value="5" id="expect_4" <?php if(isset($_POST['expect']) && $_POST['expect'] == '5'): ?>checked='checked'<?php endif; ?>  />
      No, I was disappointed. The final product didn't meet my expectations at all.</label>
    <br />
  </p>
      <input name="Submit" type="submit" class="button1" value="Submit" />
  </form> 

 

 

As previously stated: Don't save PHP code in the database.

Databases are not template engines, and shouldn't be used as such. Store the files as just that, files, and use include () to show them in the correct place instead. Simplify your design, in other words, and you'll notice that this whole issue just vanishes into thin air.

I'm sorry guys but I'm totally blond right now... I totally get keeping it simple and believe it or not, that was what I was trying to accomplish. I was hoping to have 1 file (index.php) and use the database to fill it instead of 20-30 files.

 

I tried using include() but  I keep getting an error saying it cannot find the file, when I put in the full URL it give me a security error. The includes I have on the main site (rlaartistry.com) work ok, but it won't work on the subdomain (customercenter.rlaartistry.com) and yes I saved the file in the subdomain folder.

 

I do appreciate all the feedback and I will rework the site and take the PHPs out of the database.

 

If anyone has any links or other suggestions, please let me have it.

 

Thank you again

include () works on the local path, not the URL which the web server creates. Keep the files that should be included in a folder, and point PHP to that folder using a path relative to the file that does the inclusion. Sounds like you're trying to include a file from your main site in your subdomain, without adjusting for the difference in the local path on the server itself.

In short: PHP sees what your FTP client sees, so make sure the paths and filenames are consistent with that in mind.

 

If that's not the problem, please provide some sample code, file locations and actual error messages.

Thank you for your response  ChristianF. I am taking you and Thorpe advice and removing all PHP codes from my database. Unfortunately it isn't going as fast as I wanted it to, but when I'm done I will let you know the out come.

 

Thank you again

I have successfully clean up my database and removed all PHP coding from it. Now I have another problem. I am trying to create a script that will show the percentage of satisfied customers from the survey I posted earlier. There is one question in the survey that is really significant to this and it is question number 3 (Q3). The answers for Q3 have values 1-5, where 1 & 2 = satisfied and 3-5 = dissatisfied.

 

I want to use the sum of the amount of satisfied values (satvalue1 & 2);  divide it by the number of participants (NOP) then multiply by 100 to get the percentage. So far I got the number of rows for the NOP but cant get the correct value for Q3.

 

$nop = "";
$Q3 = "";
$satvalue1 = "1";
$satvalue2 = "2";
$TotalsatValue = ($satvalue1) && ($satvalue2);
$sqlCommand = "SELECT NOP FROM survey1"; 
$value = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$satvalueTotal = mysqli_num_rows($value);
$nopvalueTotal = mysqli_num_rows($value);
$ratings = ($satvalueTotal/$nopvalueTotal)*100;
echo "$ratings%";
mysqli_free_result($value); 

 

I'll appreciate any help.

Thank you

I think you need to go over that particular bit of code again, and re-examine what it is you're asking PHP to do there. I can tell you right away that you're not doing what you think you're doing.

 

What I get from your code is that you're confused about what the different functions do, as well as what you actually want to do. You know what the end goal is, but the steps you need to take to get there. Starting to write code with that base is called "programming by coincidence", and is almost assured to lead to buggy and problematic code.

That's why I'd recommend you to step back a bit, really think through what you want to accomplish and how to do it. Then write down a short keyword list to describe the steps needed. Once you've done that you should have a clear understanding of what your application needs to do, and thus find the code trivial to write.

 

Quick example of such a list:

Retrieve count from database, where (response == 1 or response == 2) and question == 3
Retrieve count of total answers from database, where question == 3
Divide count_satisfied with count_total, and multiply with 100
Show result as %.

 

You could even have the database calculate the percentage for you, using a single query only. Which would, most likely, be the preferable solution in this case. However, since that adds complexity to the SQL query bit, and I gather that you're even less experienced with it than with PHP, I recommend getting comfortable with the above solution first.

Hey ChristianF, I really appreciate all the help you have given me. I did take your advice (I hope I executed it correctly). I got the correct value for my Q3 but I cannot get my NOP to give me the correct value. There are 10 entries in the database, NOP is auto increment and Q3 = 7. I've Googled and tried everything possible for 2 days and Im not getting any further. I am neither comfortable with PHP or MYSQL but by working with PHP over the years I kinna get an understanding of it.

Give me some guidelines as to what I am missing and how to get the NOP display the correct value. When I am done with this I was wondering if you could also share with me that bit of code to get the database calculate percentage for me.

 

Thank you

$nop = "";
$Q3 = "";
/* Retrieve count from database, where (response == 1 or response == 2) and question == 3. Not familiar with count so I used select */
 $sql_satvalue = mysql_query("SELECT NOP, Q3 FROM survey1 WHERE (Q3='1' OR Q3='2')");
/* Retrieve count of total answers from database, where question == 3*/     
 $satvalueTotal = mysql_num_rows($sql_satvalue);
 while($row = mysql_fetch_array($sql_satvalue)){
/* Here is where Im stuck. When echo $nop I get a value of 9 when it should be 10. My satvalueTotal is correct just my NOP is off*/
				$nop = $row['NOP'];
 }

What does the raw data and the tables look like, and what is the result of the query?

Also, as a few of additional exercises for you: Why are you looping over the rows returned from the database, and why are you overwriting $nop on every iteration (without doing anything else with it), why do you think that mysql_num_rows () gives you the number of rows with answers for the third question?

 

In ending, if you only want the count of the number of rows, then you might want to look up MySQL's COUNT() function. It's quite clearly explained in the MySQL manual.

You know what you want to do, now you only need to figure out how to do it, and why. That does require some research, pondering, and reflection on ones choices. ;) Don't be fooled by the seemingly irrelevance of the "whys" behind your choices, as ensuring that you're doing something for the right reasons is what really makes or breaks you application.

____________________________________________________________________________

|      NOP      |          Q1          |            Q2        |            Q3        | IP ADDRESS    |    DATE        |

|__________|____________ |_____________|_____________|____________ |___________|

|                    |                        |                          |                          |                        |                      |

|__________|____________ |_____________|_____________|____________ |___________|

 

NOP is auto_increment and Q1, Q2 & Q3 are the 3 questions, IP ADDRESS  is collected to check if that person has already participated in the survey and DATE is date.

 

Answer value for question 3 = 1-5, where 1 & 2 are favorable responses, hence a satisfied customer. Answer values 3, 4, 5 are negative response, hence, dissatisfied customers.

 

Collect the total of satisfied values:

$sql_satvalue query = gives the total of satisfied customers

The while loop was an attempt to get NOP (which didn't work).

I was echoing the $nop to check if I was getting the correct value for NOP (that will later be removed).

I thought mysql_num_rows() would give me the number of row with the values set in $sql_satvalue... WHERE (Q3='1' OR Q3='2').

 

BTW, thanks for your end notes. I will read up on COUNTS.

 

Thank you

 

Hmm... Not quite what I had in mind, but should probably have been clearer about it. What I was looking for were the results of SELECT * FROM survey1 and DESCRIBE survey1. In any case, what you posted did give me enough information, just about.

Only one more question about the structure of the data: Is it possible for someone to avoid answering question 3? If the answer is no, then you're not really looking for the number of people who've answered Q3, but the number of people who've taken the survey. One could be forgiven to think that it's a trivial detail, but the devil's in the details. ;)

 

Also, here's what the code you posted above actually does:

 // Fetch NOP and Q3 value for all rows where Q3 is either 1 or 2. In the order they were inserted into the database.
$sql_satvalue = mysql_query("SELECT NOP, Q3 FROM survey1 WHERE (Q3='1' OR Q3='2')");

// Get the number of rows fetched by above query (where Q3 is either 1 or 2).
$satvalueTotal = mysql_num_rows($sql_satvalue);

// Loop through the results, inserting each row of fetched data into $row ($row['NOP'] and $row['Q3'])
while($row = mysql_fetch_array($sql_satvalue)){
    // Set the auto_increment value to a variable, overwriting any previous values.
    $nop = $row['NOP'];
}

 

So, as you can see, you've only really accomplished the first step. Adding an, as far as I can understand, unnecessary loop to the mix. If you want to check the results of the query, using print_r ($row) inside the loop would have been a far better choice.

Hey ChristianF, thanks for your reply.

To answer your question, no. It wouldn't be possible for anyone to avoid answering any of the questions. Question 1 is used to evaluate question 3 and question 2 is just a buffer. The psychology behind it is to separate emotions from logics. If a customer is highly likely to recommend our service, however, was disappointed with the website, then that would indicate that no thought was put into answering the questions (they just randomly clicked on the answers). I don't think one can write a script that can determine ones frame of mind so this is something that a person has to do.

 

Steps 1 was what I was going for... Step 2, I also wanted to fetched NOP... this is where I lost it. My intention for step 3 was to give me the total number of NOP.

 

I visited the link you gave me and is playing around with COUNT to see if I can get a good understanding of it.

 

In that case I'd count the number of rows in total (number of total answers), and use it to divide the number of answers you got from the first query. What you want with NOP I don't quite understand, as it's simply a identifier for the individual rows.

What you want with NOP I don't quite understand, as it's simply a identifier for the individual rows.

 

NOP is what I was using to get my number of rows, but now that you mention it, it doesn't make sense to me either. Let me use up my COUNT knowledge and see what i get.

 

Thanks christian F, BRB.

OK, I tried COUNT but for some reason the total the exercise on the site got I wasn't. Since I don't know how to get both Q3 (1 or 2) and Q3 total

from one sql_query, I decided to do 2 and get the percentage from there. I vary the quantity of Q3 1s and 2s and the answer is correct.

 

My concern now is; is it efficient, will it be slow?

 

$nop = "";
$Q3 = "";
/* Retrieve from database, where (Q3 = 1 or Q3 = 2)*/
 $sql_satvalue = mysql_query("SELECT * FROM survey1 WHERE (Q3='1' OR Q3='2')");
 $satvalue = mysql_num_rows($sql_satvalue);

/* Retrieve from database survey1 total */
 $sql_surveyTotal = mysql_query("SELECT * FROM survey1");
 $surveyTotal = mysql_num_rows($sql_surveyTotal);

$satpercent = $satvalue/$surveyTotal*100;
echo "$satpercent %";

It's not very efficient, no, but unless you get a whole lot of answers it won't be too slow.

 

However, the more efficient method of doing this would be as follows:

SELECT (SELECT COUNT(*) FROM survey1 WHERE Q3=1 OR Q3=2) / (SELECT COUNT(*) FROM survey1) * 100 AS `percent`

 

What you see here is two sub-queries and one alias (AS `percent`). While the two sub queries fetch the raw numbers, the outer query is responsible for handling the calculation and returns the result labeled as "percent".

Not tested, but it should work. Though, I'm sure some of the SQL expects can come up with a better solution that that too. ;)

 

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.