php code not working after php.ini file reset - php

My service provider reset my php.ini file on me. My php code has not changed but now none of my functions are working?
I am running php.ini 5.2 what would I need to turn on, or off, to get the following code to work again?
Thank you in advance for your help
function characterListPost() {
global $wpdbNew;
$q = "SELECT id, ch_position, ch_name, ch_image, ch_description, ch_age, ch_like, ch_dislike FROM characters ORDER BY ch_position";
$rows = $wpdbNew->get_results($q,ARRAY_A);
// start with nonsense value to force a heading
$previous_season = 0;
$outputTwo='';
$i = 1;
foreach ($rows as $row) {
$outputTwo.= "<div class=\"characterbox\" id=\"div{$i}\">";
$i++;
$outputTwo.= "<div class=\"ch_name\">{$row["ch_name"]}</div>";
$outputTwo.= "<div><image class=\"ch_image\" id=\"ch_image{$row["id"]}\" alt=\"character image TBA\" src=\"{$row["ch_image"]}\" /></div>";
$outputTwo.= "<div class=\"ch_description\"><p>{$row["ch_description"]}</p></div>";
$outputTwo.= "<div class=\"ch_age\"><b>Age:</b> {$row["ch_age"]}</div>";
$outputTwo.= "<div class=\"ch_like\"><b>Like:</b> {$row["ch_like"]}</div>";
$outputTwo.= "<div class=\"ch_dislike\"><b>Dislike:</b> {$row["ch_dislike"]}</div>";
$outputTwo.= "<div class=\"Down10px clear\"></div>";
$outputTwo.= "</div>";
}
// echo test successful but $outputTwo will not display?
echo 'Connected successfully';
return $outputTwo;
}
?>
I tested the db code and it works so by process of elimination the problem must be between my php.ini file and the function code above
<?php
$wpdbNew = new wpdb('myacc.myhost.com', 'myusername', 'mypassword', 'mydbname');
if (!$wpdbNew) {
die('Could not connect: ' . mysql_error());
}
The following are already On inside php.ini
allow_url_fopen = On
allow_url_include = On
register_long_arrays = On
register_globals = On
Second part, based on provided answer below, getting the following code to work
EDIT got it to work by removing foreach ($rows as $row) {
All set, everything is working!
<?php
episodeListPost();
function episodeListPost() {
$host = 'mydomain.com';
$user = 'myusername';
$pass = 'mypassword';
$data = 'dbname';
$cn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($data, $cn) or die(mysql_error());
$sql = "SELECT id, season_num, temp_eps_num, eps_num, title, inspired, descrip FROM season ORDER BY season_num, temp_eps_num";
$result = mysql_query($sql, $cn) or die(mysql_error());
if($result) {
$previous_season = 0;
$outputOne='';
$i = 1;
while($row = mysql_fetch_assoc($result)) {
foreach ($rows as $row) {
$season = $row["season_num"];
if ($season != $previous_season){
$outputOne.= "<div class=\"seasonTitle\">Season $season</div>";
$previous_season = $season;
}
$outputOne.= "<div class=\"clear\">Episode: {$row["eps_num"]}</div>";
$outputOne.= "<div class=\"epsTitle\">Title: <span class=\"epsTitleOutput\">{$row["title"]}</span></div><div class=\"epsInsp\"> {$row["inspired"]}</div>";
$outputOne.= "<div class=\"epsDiscrip\">{$row["descrip"]}</div>";
$outputOne.= "<div class=\"Down10px\"></div>";
}
if($i == 1) { }
echo $outputOne;
mysql_free_result($result);
} else {
echo 'No results';
}
mysql_close($cn);
}
?>

The the following function. Most likely, your query is not returning any results, or the function $wpdbNew->get_results() is not returning any records.
function characterListPost() {
global $wpdbNew;
$q = "SELECT id, ch_position, ch_name, ch_image, ch_description, ch_age, ch_like, ch_dislike FROM characters ORDER BY ch_position";
$rows = $wpdbNew->get_results($q,ARRAY_A);
// start with nonsense value to force a heading
$previous_season = 0;
$outputTwo='';
$i = 1;
foreach ($rows as $row) {
$outputTwo.= "<div class=\"characterbox\" id=\"div{$i}\">";
$i++;
$outputTwo.= "<div class=\"ch_name\">{$row["ch_name"]}</div>";
$outputTwo.= "<div><image class=\"ch_image\" id=\"ch_image{$row["id"]}\" alt=\"character image TBA\" src=\"{$row["ch_image"]}\" /></div>";
$outputTwo.= "<div class=\"ch_description\"><p>{$row["ch_description"]}</p></div>";
$outputTwo.= "<div class=\"ch_age\"><b>Age:</b> {$row["ch_age"]}</div>";
$outputTwo.= "<div class=\"ch_like\"><b>Like:</b> {$row["ch_like"]}</div>";
$outputTwo.= "<div class=\"ch_dislike\"><b>Dislike:</b> {$row["ch_dislike"]}</div>";
$outputTwo.= "<div class=\"Down10px clear\"></div>";
$outputTwo.= "</div>";
}
// echo test successful but $outputTwo will not display?
echo 'Connected successfully.';
if($i == 1) { echo '<br />' . 'No Rows Found'; } else { echo '<br />' . $i . ' Rows Found'; }
return $outputTwo;
}
SECOND TEST
<?php
characterListPost();
function characterListPost() {
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$data = 'test';
$cn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($data, $cn) or die(mysql_error());
$sql = "SELECT id, ch_position, ch_name, ch_image, ch_description, ch_age, ch_like, ch_dislike FROM characters ORDER BY ch_position";
$result = mysql_query($sql, $cn) or die(mysql_error());
if($result) {
$previous_season = 0;
$outputTwo='';
$i = 1;
while($row = mysql_fetch_assoc($result)) {
$outputTwo.= "\n\n" . '<!--- ROW #' . $i . ' -->' . "\n";
$outputTwo.= '<div class="characterbox" id="div' . $i . '">' . "\n";
$i++;
$outputTwo.= '<div class="ch_name">' . $row["ch_name"] . '</div>' . "\n";
$outputTwo.= '<div><image class="ch_image" id="ch_image' . $row["id"] . '" alt="character image TBA" src="' . $row["ch_image"] . '" /></div>' . "\n";
$outputTwo.= '<div class="ch_description"><p>' . $row["ch_description"] . '</p></div>' . "\n";
$outputTwo.= '<div class="ch_age"><b>Age:</b> ' . $row["ch_age"] . '</div>' . "\n";
$outputTwo.= '<div class="ch_like"><b>Like:</b> ' .$row["ch_like"] . '</div>' . "\n";
$outputTwo.= '<div class="ch_dislike"><b>Dislike:</b> ' . $row["ch_dislike"] . '</div>' . "\n";
$outputTwo.= '<div class="Down10px clear"></div>' . "\n";
$outputTwo.= '</div>' . "\n";
}
if($i == 1) { echo '<br />' . 'No Rows Found'; } else { echo '<br />' . $i . ' Rows Found'; }
echo '<textarea>' . $outputTwo . '</textarea>';
mysql_free_result($result);
} else {
echo 'No results';
}
mysql_close($cn);
}
?>
HACK AROUND
Replacing the function with this should work. Do not forget to call 'characterListPost()' wherever this is supposed to be output.
function characterListPost() {
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$data = 'test';
$cn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($data, $cn) or die(mysql_error());
$sql = "SELECT id, ch_position, ch_name, ch_image, ch_description, ch_age, ch_like, ch_dislike FROM characters ORDER BY ch_position";
$result = mysql_query($sql, $cn) or die(mysql_error());
$return = "";
if($result) {
$previous_season = 0;
$outputTwo='';
$i = 1;
while($row = mysql_fetch_assoc($result)) {
$outputTwo.= "<div class=\"characterbox\" id=\"div{$i}\">";
$i++;
$outputTwo.= "<div class=\"ch_name\">{$row["ch_name"]}</div>";
$outputTwo.= "<div><image class=\"ch_image\" id=\"ch_image{$row["id"]}\" alt=\"character image TBA\" src=\"{$row["ch_image"]}\" /></div>";
$outputTwo.= "<div class=\"ch_description\"><p>{$row["ch_description"]}</p></div>";
$outputTwo.= "<div class=\"ch_age\"><b>Age:</b> {$row["ch_age"]}</div>";
$outputTwo.= "<div class=\"ch_like\"><b>Like:</b> {$row["ch_like"]}</div>";
$outputTwo.= "<div class=\"ch_dislike\"><b>Dislike:</b> {$row["ch_dislike"]}</div>";
$outputTwo.= "<div class=\"Down10px clear\"></div>";
$outputTwo.= "</div>";
}
// if($i == 1) { echo '<br />' . 'No Rows Found'; } else { echo '<br />' . $i . ' Rows Found'; }
$return = $outputTwo;
mysql_free_result($result);
// } else {
// echo 'No results';
}
mysql_close($cn);
return $return;
}

Related

Displaying Data Randomly in PHP From Database

I'm Building a Simple PHP Page for MCQS (Multiple Choice Questions). I'm Using MySQL Database to retrieve data. I want to Display Options / Alternatives ( i.e: field_correct_answer_value, field_wrong_answer_1_value, field_wrong_answer_2_value, field_wrong_answer_3_value, field_wrong_answer_4_value) in Random Order with stylesheet so i can identify which one is correct.
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
echo "<div id=a1>". $row["field_correct_answer_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_1_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_2_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_3_value"]."</div>";
echo "<div id=a2>". $row["field_wrong_answer_4_value"]."</div>";
}
Is it Possible?
($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//FOR RANDOMIZATION OPTION COLLECTING DATA IN ARRAY AND SHUFFLING THEM
$corr = "<div id=a1>" . $row["field_correct_answer_value"] . "</div>";
$wor1 = "<div id=a2>" . $row["field_wrong_answer_1_value"] . "</div>";
$wor2 = "<div id=a2>" . $row["field_wrong_answer_2_value"] . "</div>";
$wor3 = "<div id=a2>" . $row["field_wrong_answer_3_value"] . "</div>";
$wor4 = "<div id=a2>" . $row["field_wrong_answer_4_value"] . "</div>";
$opt = array($corr, $wor1, $wor2, $wor3, $wor4,);
$opt_ran = shuffle($opt);
echo "<hr><div id=q1>". $row["field_question_value"]."</div>";
//FOR DISPLAYING RANDOM OPTIONS
foreach ($opt as $number) {
echo $number;
}
}

Issues getting PHP shuffle to output results

I'm attempting to shuffle the users that I have in my database and then output the first and last name of those results. I have PHP error coding in this file and it is not throwing any errors. Just nothing is outputting not even..
if ($shuffle_firstname == true) {
echo $shuffle_firstname . $shuffle_lastname;
} else {
echo "No users have been registered yet.";
}
Does anyone see what I'm doing wrong?
$con = mysqli_connect("localhost", "root", "", "db");
$shuffle_run = mysqli_query($con,"SELECT * FROM users WHERE `group`= 3");
$shuffle_numrows = mysqli_num_rows($shuffle_run);
if( $shuffle_numrows > 0) {
while($shuffle_row = mysqli_fetch_assoc($shuffle_run)){
$shuffle_id = $shuffle_row['id'];
$shuffle_firstname = $suffle_row['firstname'];
$shuffle_lastname = $shuffle_row['lastname'];
$shuffle_username = $shuffle_row['username'];
$shuffle_email = $shuffle_row['email'];
if ($shuffle_firstname == true) {
echo $shuffle_firstname . $shuffle_lastname;
} else {
echo "No users have been registered yet.";
}
}
}
if(isset($_POST['shuffle'])) {
$shuffle_row = array();
shuffle($shuffle_row);
foreach ($shuffle_row as $shuffle) {
echo $shuffle_firstname . " " . $shuffle_lastname;
}
}
?>
<input type="submit" value="Shuffle" name="shuffle">
You are overwriting your results with an empty array, then you use shuffle on an empty array, then you're looping the empty array using foreach. I've cleaned up the code, could you try this?
<?php
$con = mysqli_connect("localhost", "root", "", "db");
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");
echo 'Normal results: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
$array[] = $row;
echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
if (isset($_POST['shuffle'])) {
shuffle($array);
echo 'Shuffled results: <br>';
foreach ($array as $result) {
echo $result['firstname'] . ' ' . $result['lastname'] . '<br>';
}
// echo $results[0]['firstname'] . ' ' . $results[0]['lastname']; // To display one random result
}
?>
<form method="post">
<input type="submit" value="Shuffle" name="shuffle">
</form>

For each results- Mysql - JSON

How i separate the first result of for each loop and remaining. I have 2 divs, i want first result to be displayed there and rest on another div.
Also is there any way that i can get json decode without for each loop, i want to display result based on for each values from database, and querying database in for each loop is not recommended.
Here is my code, What i want
<div class="FirstDiv">
Result1
</div>
<div class="RemDiv">
Remaining result from for each loop
</div>
Here is full code
$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
if (++$i == 6)
break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if (mysqli_num_rows($rs)==1) //uid found in the table
{
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
}
Try this:
$data = json_decode($response->raw_body, true);
$i = 0;
echo '<div class="FirstDiv">'; // add this line here
foreach( $data['photos'][0]['tags'][0]['uids'] as $value ) {
if (++$i == 6) break;
$check = "SELECT fullname FROM test_celebrities WHERE shortname = '$value[prediction]'";
$rs = mysqli_query($con,$check);
if ( mysqli_num_rows($rs) == 1 ) { //uid found in the table
$row = mysqli_fetch_assoc($rs);
$fullname= $row['fullname'];
}
// Echo celebrity information:
echo 'Celebrity Name: ' . $fullname . '<br/>';
echo 'Similar: ' . $value['confidence']*100 .'%'. '<br/><br/>';
echo "<img src='actors/$value[prediction].jpg'>";
echo "<hr/>";
if ($i==1) { echo '</div><div class="RemDiv">'; }; // add this line here
}
echo '</div>'; // close the last tag
$predictions=array();
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
$predictions[]="'" . mysqli_real_escape_string($con, $value[prediction]) . "'";
}
$check="SELECT fullname FROM test_celebrities WHERE shortname IN (" . implode(',' $predictions) . ")";
$rs = mysqli_query($con,$check);
while ($row = mysqli_fetch_assoc($rs)) {
if (!$count++) {
// this is the first row
}
But note that you now have two sets of data which are sorted differently - hence you'll need to iterate through one and lookup values in the other.

Echo PHP array as HTML?

Okay, so I've literally spent all day trying to achieve this and have gotten nowhere.
I am creating a flipbook (using turn.js).
I have names and cause of death stored in a MySQL database.
I want to print 5 names and their cause of death on each page.
To add a page in turn.js, all i have to do is add a in
So i load the name and cause of death values into arrays, and then echo a whole div however with the array variable.
I'm really having trouble getting this into code.
At the moment I have managed to get this onto seperate pages, but how would i get it to print only 5 on a page, and then create a new page (untill there are no more names)
Here is what i have so far.
<?php
$dbc = mysql_connect('localhost', 'xxxx', 'xxxxxxx');
if (!$dbc) {
die('Not Connected: ' . mysql_error());
}
$db_selected = mysql_select_db("xxxxxx", $dbc);
if (!$db_selected)
{
die ("Can't Connect : " . mysql_error);
}
$query = "SELECT * FROM TABLE";
$result = mysql_query($query);
$victim = array();
$cod = array();
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
$victim['$count'] = $row['Victim'];
$cod['$count'] = $row['COD'];
$count++;
}
?>
<div id="deathnote">
<div style="background-image:url(images/coverpage.jpg);"></div>
<div style="background-image:url(images/page1.jpg);"></div>
<div style="background-image:url(images/page2.jpg);"></div>
<div style="background-image:url(images/page3.jpg);"></div>
<div style="background-image:url(images/page4.jpg);"></div>
<div style="background-image:url(images/page5.jpg);"></div>
<?php
for ($x=0; $x=$count; $x++) {
echo "<div style='background-image:url(images/page5.jpg);'></div><div class='content'><div class='name'>" . $victim['$x'] . "</div><div class='cod'>" . $cod['$x'] . "</div></div></div>";
}
?>
Any help will be GREATLY appreciated.
Thanks in advance, guys :)
Take a look at the $_GET variable http://php.net/manual/de/reserved.variables.get.php there you can set variables in the URL like http://lol.de/?form=1&to=5
$from = $_GET['from'];
$to = $_GET['to'];
if(!is_numeric($from) || !is_numeric($to)) die('SQL Injection');
$count = $to - $from;
$query = "SELECT * FROM TABLE LIMIT ".$from-",".$count;
Then you create a dynamic link, where you add 5 to $from and $to.
Not tested, just a Idea.
Check the last two line on your code, e.g.:
for ($x=0; $x=$count; $x++) {
echo "<div style='background-image:url(images/page5.jpg);'></div><div class='content'><div class='name'>" . $victim['$x'] . "</div><div class='cod'>" . $cod['$x'] . "</div></div></div>";
Change them to:
$onPage = 5;
$currentPage = 1;
for ($x=0; $x=$count; $x++) {
if (($x % $onPage) == 0)
{
echo "<div style='background-image:url(images/page" . $currentPage . "jpg);'></div>";
$currentPage++;
}
echo "<div class='content'>"
. "<div class='name'>" . $victim['$x'] . "</div>"
. "<div class='cod'>" . $cod['$x'] . "</div>"
. "</div>";
}
You can play around with the "modulus" operator - %, to achieve the result you need.
Try below code.
$recordPerPage = 5;
<?php
for ($x=1; $x<=$count; $x++) {
if($x <= $recordPerPage ) {
echo "<div style='background-image:url(images/page".$x.".jpg);'></div><div class='content'><div class='name'>" . $victim['$x'] . "</div><div class='cod'>" . $cod['$x'] . "</div></div></div>";
}
$x = $recordPerPage;
$recordPerPage += $recordPerPage;
}
?>
Hope this will help to you!
Cheers!
This may help you.
<?php
$recordPerPageCount = 5;
$pageCount = 1;
for ($x=0; $x<$count; $x++) {
if($x % $recordPerPageCount == 0) {
//This block will execute each time after diplaying five records in a page
$pageCount++;
}
echo "<div style='background-image:url(images/page".$pageCount.".jpg);'></div><div class='content'><div class='name'>" . $victim['$x'] . "</div><div class='cod'>" . $cod['$x'] . "</div></div></div>";
}
?>

Displaying data from database in table

I am trying to display data in table form with 3 columns. Each should have a main category with some drop down lists. I get all the information to display but all is in one column and the drop down information does not display with the correct heading.
echo "<table>";
while ($row = mysql_fetch_array($result)) {
$count = 1;
if ($count = 1) {
$sCatID = ($row['CatID']);
echo "<tr valign='top'><td><b><a href='#" . $sCatID . "'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $sSub . "</a><br>";
echo "</td>";
}
$count = 2;
} elseif ($count = 2) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='#'>" . $row2['Sub'] . "</a><br>";
echo "</td>";
}
$count = 3;
} elseif ($count = 3) {
$sCatID = ($row['CatID']);
echo "<td><b><a href='.$sCatID.'>" . $sCatID . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
$sSub = ($row2['CatID']);
$sSubID = ($row2['SubID']);
echo "<dd><a href='.$sSub.'>" . $sSub . "</a><br>";
echo "</td></tr>";
}
$count = 1;
}
}
if ($count = 2) {
echo "<td> </td><td> </td></tr>";
} elseif ($count = 3) {
echo "<td> </td></tr>";
}
echo "</table>";
It doesn't seem to close the rows and table correctly... And it is also putting some of the drop down items before it displays the first heading.
If i display it in only one column it is working fine.
You should use == instead of single = in your if statements. Else it would execute everytime as that condition is always true.

Categories