You basically need another ajax call passing values to a php script that will insert/update database.
dxnitz commented: thanks its work +0
LastMitch commented: Thanks for the resources! +10
LastMitch commented: Nice Work! +10
You basically need another ajax call passing values to a php script that will insert/update database.
Check for existence of image in the DB table and if exists asign it to $logo variable otherwise assign a placeholder to $logo variable. Then display whatever is in the $logo.
<?php
if(isset($row_getListing['image'])) {
$logo = $row_getListing['image'];
} else {
$logo = 'placeholder.jpg';
}
?>
<div id="logo"><img src="images/<?php echo $logo; ?>" width="114" height="57" /></div>
Try using htmlspecialchars function which will replace < and > with their html codes.
if ($content !== false) {
echo htmlspecialchars($content);
}
AHarrisGsy is right, upvote for that. I'll just to simplyfy it a little bit:
if(isset($_SESSION['SESSION_NAME'])) {
echo '<a href = "/Logout.php" > Logout </a>';
} else {
echo '<a href = "/Login.php" > Login </a>';
}
You are welcome.
Put the php code before the html, that will check whether either of the forms was submitted and what the selected value was:
<?php
// if either of submit buttons was pressed, $_POST['submit'] would be set
// and you can check the selected values
if(isset($_POST['submit'])) {
// the selected value of the first select element (dropdown)
$word1 = $_POST['Words'];
// the selected value of the second select element (dropdown)
$word2 = $_POST['Words2'];
// do what you want with selected values
...
}
?>
You could also set different names to the submit buttons if you wanted to have more control.
Do have any links or examples that show how mysqli_num_rows() & mysqli_fetch_assoc() works in OOP style.
There are good examples on php.net (see the Examples section after the definitions):
Class initialization
http://www.php.net/manual/en/mysqli.construct.php
Querying (includes code for class initialization)
http://www.php.net/manual/en/mysqli.query.php
Getting number of rows (includes code for class initialization and querying)
http://www.php.net/manual/en/mysqli-result.num-rows.php
php.net site at first look seems to be a bit unfriendly to new users due to a lot of technical information. But once you get used to it you find it the best resource for PHP. Always have look at the examples section and See also section. User comment can be interesting, too.
Considering the above, the Posts class should be something like:
class Posts {
private $db;
public function __construct($mysqli) {
include 'dbConnMysqli.php';
$conn = new dbConnMySqli;
$this->db = $conn->getmysqliObject();
}
public function getPosts(){
$query = "SELECT * FROM posts";
$sql = $this->db->query($query);
$result = $sql->fetch_assoc();
$row = $result->num_rows;
return $result;
}
}
There are many questions here, I hope I manage to follow the main track :-). I will do it in couple of posts so we stay organized (in other words: you can not swallow a whole watermellon, you have to cut it in pieces).
I think foo_mysqli class is unnecessary here for two reasons: 1. you are not really adding anything to the mysqli class (which is a purpose of extending an existing class) but that would be okay; 2. you are not using it anywhere, you are using mysqli class in other scripts.
I am not saying that you should not do it this way. You can extend mysqli but then try to add some useful functionality to it like various ways of handling errors (display the errors during development, log the errors in production, provide default connection parameters etc). The following is the class I use for connecting and you can use it as an example (see the comments in it):
/**
* This class establishes a connection to Mysql database using mysqli extension
*/
class dbConnMySqli
{
// connection parameters
protected $host = '';
protected $user = '';
protected $pass = '';
protected $db = '';
// mysqli object
protected $mysqliObj = null;
/**
* Initialize connection parameters if set
*
* @param string host
* @param string $db_user
* @param string $db_pass
* @param string $db_name
*/
public function __construct(
$db_host = null,
$db_user = null,
$db_pass = null,
$db_name = null
)
{
if($db_host && …
I just want to know the best way of connecting beside PDO. So OOP with MYSQLI is good.
In most cases PDO is the best way to do it, but you need at least PHP 5 for it, it won't work in older versions. The good side is that it somehow makes you independent of database vendor since it supports quite a many of them, so you can change a database and still use the same code. But it won't support all missing or proprietary functionanities of different databases. There are abstraction layers that do that better (I used PEAR MDB2).
If you plan to use only MySql then mysqli exstension is equaly good.
The OOP mode is preferred (from my point of view).
Both OOP and procedural modes do their job OK. I prefer OOP way just because most (if not all) frameworks and abstraction layers are OOP and is just good if you are familiar with this approach and use it consistently.
$result = mysqli_query($link, "SELECT * FROM posts")
This is mysqli used in procedural way. There is also a OOP version of this function:
$mysqli->query("SELECT * FROM posts");
See examples of both approaches here.
You changed the mysql connection function from OOP mode to procedural mode. The OOP mode initialized a mysqli object which has methods you can call. But the procedural mode does not initialize the object so you can not call it's methods (i.e the query method). Why did you do that?. The OOP mode is preferred (from my point of view).
// for this to use you need a mysqli object
$sql = $mysqli->query("SELECT * FROM posts") or die("Problem with query");
If you do not have a mysqli object (i.e. you do it procedural style), your connection function has to return the link.
function myDbObj(){
// this does not have to be global
global $mysqli;
//connect to server and select database
$link = mysqli_connect('host', 'user', 'password', 'db');
//if the connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
// otherwise return the link
} else {
return $link;
}
}
Then you shoot the query:
$result = mysqli_query($link, "SELECT * FROM posts")
Haven't tested the code, see it here: http://www.php.net/manual/en/mysqli.query.php.
You are welcome. Happy coding.
And this is the whole script:
$username = "username";
$password = "password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br><br>";
//select a database to work with
$selected = mysql_select_db("database",$dbhandle)
or die("Could not select database");
function getStatesArrayString() {
//choose the table and return records
$result = mysql_query("SELECT id, state,country_id FROM state ORDER BY country_id");
// initialize temporary string for one row
$tempStr = '';
// initialize the string that will be returned
$retStr = '';
// current value of country_id
$country = null;
//get group
while($row = mysql_fetch_assoc($result)) {
// we are starting new country
if ($row['country_id'] != $country) {
// echo the array string only when $country is not null
// (which is only on beginning)
if($country != null) {
// first eplace trailing , with ]
$tempStr = substr_replace($tempStr, ']', -1, 1);
// now add the tempoprary string to return string
// semicolon and newline chars were added for javascript
$retStr .= $tempStr . ";\n";
}
$country = $row['country_id'];
$tempStr = "[$country] = parent_array [";
}
// we are appending to the existing country
$tempStr .= "'{$row['id']}|{$row['state']}',";
}
// once finished return the return string
return $retStr;
}
// test what function returned
echo '<pre>' . getStatesArrayString() . '</pre>';
//end of group
$db = null;
echo "<br>Fetched data successfully\n<br>";
mysql_close($conn);
?>
The output I get is:
[6] = parent_array ['70|Andorra la Vella'];
[8] = parent_array ['3|Abu Dhabi','16|Ajman'];
[19] = parent_array ['79|Antwerpen'];
[24] = parent_array ['32|al-Manama'];
[31] = parent_array ['60|Amazonas','48|Alagoas','5|Acre','59|Amapá'];
[38] = parent_array ['50|Alberta'];
[41] = parent_array ['78|Antofagasta'];
[42] = parent_array ['72|Anhui'];
[46] = parent_array ['77|Antioquia'];
[55] = parent_array ['71|Anhalt Sachsen'];
[60] = parent_array ['74|Annaba','53|Alger'];
[62] = parent_array ['26|al-Daqahliya','27|al-Faiyum','28|al-Gharbiya','42|al-Sharqiya','51|Aleksandria','38|al-Qalyubiya','33|al-Minufiya','25|al-Buhayra','34|al-Minya'];
[65] = parent_array ['67|Andalusia','88|Aragonia'];
[67] = parent_array ['61|Amhara','7|Addis Abeba'];
[71] = parent_array ['56|Alsace','86|Aquitaine'];
[76] = parent_array ['11|Adzaria [Atšara]','1|Abhasia [Aphazeti]'];
[77] = parent_array ['100|Ashanti'];
[97] = parent_array ['4|Aceh'];
[98] = parent_array ['68|Andhra Pradesh'];
[101] = parent_array ['90|Ardebil'];
[102] = parent_array ['20|al-Anbar','44|al-Tamim','43|al-Sulaymaniya','35|al-Najaf','37|al-Qadisiya'];
[105] = parent_array ['2|Abruzzit','82|Apulia'];
[107] = parent_array ['45|al-Zarqa','62|Amman'];
[108] = parent_array ['15|Aichi','81|Aomori','18|Akita'];
[109] = parent_array ['84|Aqtöbe','54|Almaty','55|Almaty Qalasy'];
...
First to let you know that I do not know Joomla at all.
You can prepare a function that will return the string, representing the arrays, appropriate for your javascript. My suggestion is the following:
function getStatesArrayString() {
//choose the table and return records
$result = mysql_query("SELECT id, state,country_id FROM state ORDER BY country_id");
// initialize temporary string for one row
$tempStr = '';
// initialize the string that will be returned
$retStr = '';
// current value of country_id
$country = null;
//get group
while($row = mysql_fetch_assoc($result)) {
// we are starting new country
if ($row['country_id'] != $country) {
// echo the array string only when $country is not null
// (which is only on beginning)
if($country != null) {
// first eplace trailing , with ]
$tempStr = substr_replace($tempStr, ']', -1, 1);
// now add the tempoprary string to return string
// semicolon and newline chars were added for javascript
$retStr .= $tempStr . ";\n";
}
$country = $row['country_id'];
$tempStr = "[$country] = parent_array [";
}
// we are appending to the existing country
$tempStr .= "'{$row['id']}|{$row['state']}',";
}
// once finished return the return string
return $retStr;
}
This function will need a database connection already established (or you can add that to the function). The function will return a string that will represent a javascript code that assigns the to the array. You can change the function once the database changes.
To test the function do the following:
echo '<pre>' . getStatesArrayString() . '</pre>';
Can you post the state table contents in a CREATE TABLE / INSERT INTO statement (you get it by exporting the table into SQL format from phpmyadmin). Also please post the whole php/html code. I am sort of guessing whatyou want but would prefer to test it in my environment. You can also PM the code if you prefer.
Insert some debug code say after line 89:
echo "Username query: $query_checkuser<br />";
echo "Password query: $query_checkpswd<br />";
echo "Entered password: $password<br />";
echo "Password in the DB: $p_password<br />";
and check whether queries and values are correct.
Can you post the state table contents in a CREATE TABLE / INSERT INTO
statement (you get it by exporting the table into SQL format from phpmyadmin). Also please post the whole php/html code. I am sort of guessing whatyou want but would prefer to test it in my environment. You can also PM the code if you prefer.
When you call the function you have to pass it a parameter.
// this is a function declaration only
function getPosts($mysqli) {
$sql = $mysqli->query("SELECT * FROM posts") or die("Problem with query");
while($row = mysqli_fetch_assoc($sql)) {
echo $row['Content'];
}
}
// now, to use the function first initialize the mysqli object
$myDbObj = new $mysqli('host', 'user', 'password', 'db');
// then call the function with the object as a parameter
getPosts($myDbObj);
The function does not know about the mysqli object. You should either pass it as a parameter or initialize mysqli within the function (as lastMitch suggested).
$mysqli = new $mysqli('host', 'user', 'password', 'db');
function PM($mysqli) {
$query = "SELECT * FROM posts";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($query)) {
echo $row['Content'];
}
}
or
$db = new $mysqli('host', 'user', 'password', 'db');
function PM($db) {
$sql = $db->query("SELECT * FROM posts") or die("Problem with query");
while($row = mysqli_fetch_assoc($sql)) {
echo $row['Content'];
}
}
At login of each user:
- check what the user's company is
- if the subscription has expired run the update query for all the users from that company, something like:
mysql_query( "UPDATE USERS SET STATUS='LOGGED_OFF' WHERE company='$company'";
It was just an example since I am not sure if I understood your code. I will try to do an example with your code but if it doeasn't work we'll try to clarify it then.
//choose the table and return records
$result = mysql_query("SELECT id, state,country_id FROM state ORDER BY country_id");
$string .= '"'.$val.'", ';
$country = null;
// initialize $str
$str = '';
//get group
while($row = mysql_fetch_assoc($result)) {
if ($row['country_id'] != $country) {
// I do not know why these are
echo '<br><br>';
$str .= "[".($row['country_id'])."] = parentarray [";
$country = $row['country_id'];
}
// get list
$str .= "'".$row['id'] . "|" . $row['state']."',";
}
// replace trailing , with ]
$newStr = substr_replace($str, ']', -1, 1);
echo $newStr;
//end of group
$db = null;
echo "<br>Fetched data successfully\n<br>";
mysql_close($conn);
?>
i havent sleptbecause i started working on this @ midnigh
No worries mate. Just take it easy. Sometimes it is good to take a break for 10 mins or so.
For images you do not have to store the sizez (you could if you wish). You can get them using getimagesize function instead.
// calculate the width and the height or read them from db
$width = ...
$height = ...
// use the dimensions in the code
$spannedRow = '<td rowspan=2 colspan=2 height="' . $height . '" width="' . $width . '">';
$spannedRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$spannedRow .= '"></script></td>';
$normalRow = '<td height="' . $height . '" width="' . $width . '">';
$normalRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$normalRow .= '"></script></td>';
except i do need to figure out how to define what page it takes you to, and where that page is.
<?php
// define the target page using some logic
$target_page = 'some_page.php';
?>
<form method="post" action="<?php echo $target_page; ?>">
<input type="submit" name="submit" value="Click me">
</form>
Its a 40+ MB tar.gz i can ftp you if you let me... for the template that runs my live site.
No need to send entire site code. With each question just post the relevant code snippet.
Can you please explain more? or maybe even provide a basic example?
http://www.jquery4u.com/function-demos/ajax/#
http://tutorialzine.com/2009/09/simple-ajax-website-jquery/
etc...
Very many questions in such a short time. I am getting lost :-)
can you change the heading of this thread to "Create Ad Space" instead of "create a mysql polulated dropdown menu"
I cant't do that, I am not the admin.
To generate table just do something like:
$jsCode = 'http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y';
$spannedRow = '<td rowspan=2 colspan=2 height="125" width="125">';
$spannedRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$spannedRow .= '"></script></td>';
$normalRow = '<td height="125" width="125">';
$normalRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$normalRow .= '"></script></td>';
echo '<table align="center" cellpadding="0" cellspacing="0">';
// if you read data from the sb do the while loop otherwise do the for loop
while(<reading rows == true>) {
echo '<tr>';
echo $spannedRow;
echo $spannedRow;
echo $normalRow;
echo $normalRow;
echo $normalRow;
echo $normalRow;
echo '</tr>';
}
echo '</table>';
This is just an example. You have to apply your logic to it.
You have to be familiar with ajax to use it. The simplest way is to use jquery. If you are not familiar with it then I strongly suggest you learn it first, otherwise you will be dependend on others to help you with each issue and noone can guarantee a quick response. And easiest way to help someone is to see the code.
So I want to populate the javascripts in the cells, from a database.
What is the javascript code?
I also want the ads to display properly, by having PHP magically re-draw my tables
You can do that easily with PHP. What is the code for the table?
IS there a nice and easy way to re-draw said tables, based on the output,
There is nice and easy way if you do not mind reloading the page. Just send the new values using a form and GET or POST method.
If you do now want to reload the page you can use ayax but that is not that easy anymore. You should be familiar with ajax for this.
It still displays the text Partners to the left of the menu... im assuming its supposed to be there. Also that it is coming from <label for="partner">Partner</label>.
Yes.
Question #1-B: May i use things like <h1> like i can with <a href= ?
Sorry, I do not understand the question. Please explain.
Instead of echoing the parts of the string asign it to a string variable and use substr_replace function, i.e.:
$str = "parentarray ['136|Balkhs','481|Herat','564|Kabol','979|Qandahar',";
$newStr = substr_replace($str, ']', -1, 1);
See my post above.
OK, so put back the dot you removed and declare the $partner_block
variable before the while loop (so you can later append to it):
$partner_block = '';
while($partner = mysql_fetch_array( $partners ))
{
$partner_id = $partner['id'];
$partner_name = $partner['name'];
## Now, let's assign the partner inside the option form tags.
## the partner named bundagle and somewhere should be now inside the option tags.
$partner_block .= '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
} ## end while loop
If the variable has not been declared yet, you can not append to it. You can asign to it (with asigning you declare the variable). So just remove the dot (appending operator):
$partner_block = '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
That will remove the error in line 22 but I do not know if that is all that is needed for your code to work correctly. Tell us how it goes.
You are appending a string to a variable $partner_block
that is not defined anywhere else (at least not in the code you posted):
$partner_block .= '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
You are welcome. Do not forget to mark as solved when solved.
There are two small glitches:
That is why I corrected the javascript code to the following:
$(document).ready(function() {
$('.dropdown').hide();
$('.autosuggest').keyup(function (e) {
// we also have to check if user is deleting entries
// in that case less than 3 characters also trigger suggestion
isDelete = false;
// if delete or backspace were pressed,
// less than 3 characters also trigger suggestion
if( e.which == 8 || e.which == 46) {
isDelete = true;;
}
var search_term = $(this).attr('value');
// if search term does not exist or
// if it is less than 3 chars and user hasn't deleted any chars
// then hide the list
if(
search_term.length == 0 ||
(search_term.length < 3 && isDelete == false)
) {
$('.dropdown').hide();
} else {
$('.dropdown').show();
}
// check if we have at least 3 characters or user is deleting characters
if(search_term.length >= 3 || isDelete == true) {
// read the values of the select elements (drop downs)
// read the value of category
var category=$('#category').attr('value');
// this is the conditions you will post over to the searchApp.php script
// assembled here for clarity
var conditions = {
'search_term' : search_term,
'category' : category
};
// alert(conditions.toSource());
// post the conditions over to the searchApp.php script
$.post('ajax/searchApp.php', conditions, function(data) {
//alert(data);
$('.result').html(data);
/*
// you can …
Well it is only the css that has to be mended. It works for me if the div with the class="dropdown" has position absolute and high z-index value:
.dropdown {
position: absolute;
z-index: 10000;
}
You are trying to assign $pricetotal
to the $_SESSION["cart_array"]
array several times on the beginning but $pricetotal
variable is not existing yet at that point. It is getting created on line 99. Maybe you should assign it to a hidden input if you want to use it after POSTing the form.
Post the complete html, css and ajax code, plus the database structure with some data (in CREATE TABLE... and INSERT INTO.. SQL statements, so I can just copy them to phpmyadmin) and I can have a look at it tomorow if time permits. You can also send it to my PM if you prefer.
But I still have some questions, if you don't mind me. I know you are probably tired of me at this point.
No, I am quite happy to help if I can. But the problem is that I haven't used dojo in my life so I do not have a clue about how it works. You have to figure out what is the format of data that dojo chart of selected type expects and you then have to provide data in that format.
I would suggest you close this thread and start a fresh new one so others can contribute (I doubt many people are following this thread since it is so long). You give it a good title like How to create a Dojo StackedBar so Dojo gurus can jump in (hope there are any).
I am not an expert in CSS so I will give it a try but I do not claim I am 100% right.
JQuery function puts the result in the ul
element. For this reason the ul
element grows and pushes the content below it further down. If you want it not to affect the contents around it the position property should be set to absolute
while the position of the parent element (div
with class=dropdown
) should be set to relative
(so the absolute
works on ul
). Also the z-index
property of the ul
element should be set to something high, say 10000
, so you make sure the autosuggested list is above all other elements.
So you are saying that the dropdown div is pushing the content down to make room for autosuggestions? If yes, post the CSS for the dropdown class.
Without debugging tools it is like walking in a dark room. It is hard to figure out what is the format of the data passed to the charting function. Do you use Firefox? There must be a javascript console where you can get basic information about javascript code execution. I strongly recommend to install Firebug exstension though.
Do you get any javascript errors? Do you use any debugging tools like Firebug?
Please insert the following code on line 64
alert(dataEnrolled.toSource());
alert(dataExpected.toSource());
and check whether the data for dojo chart is formatted correctly (it will be displayed in two dialog boxes).