Jump to content

[SOLVED] Please help - i'm stumped


giraffemedia

Recommended Posts

I have a set of numbers that are comma separated in a table in my db. These are magazine issue numbers that were entered as an array when the user selects what issues they have read through checkboxes in a form. They simply read like 31,32,35,45,52 etc.

 

What i am trying to do is create an edit form that enables the user to change these via checkboxs when they are on the edit page. I need to pre-populate the form with a list of checkboxes that are either checked if the number is present in the array or unchecked if not.

 

I can understand the logic but cannot get my code to work. Does anyone have an idea how this would work?

 

Would I need to implode the array first and check to see if the value is equal to that of the checkbox?

 

Regards

 

James P

Link to comment
Share on other sites

Yes sorry - I was a bit vague.

 

Here is the code that i'm working on...

 


// Define the array variable

$bf_issues_booked = explode(",",$row['bf_issue_number']);

// just to see if there is any result

echo $bf_issues_booked;

// A loop used to populate my form with the checkboxes and issue name, number etc. This works fine

while($row=mysql_fetch_assoc($issues_result)) {

$issues[] = '<input name="issue_number[]" type="checkbox" value="' .$row['issue_number']. '" tabindex="11" />Issue <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] ;

} 

// A function set up to poulate a table with values returned from the above loop
function issues_table($data, $details="")
{
$sret = "<table ".$details.">\n";

$all = count($data)-1;

for($i=0; $i <= $all; $i++)
{
	if(($i % 2)==0)
	{
		$sret .= "<tr><td>".$data[$i]."</td>";
	}
	else
	{
		$sret .= "<td>".$data[$i]."</td></tr>\n";
	}
}
//	Catch if an odd cell
if(($all % 2) == 0)
{
	$sret .= "<td><br></td></tr>\n";
}

$sret .= "</table>\n";
return $sret;
}

$data = $issues;

echo issues_table($data, "border='0' width='100%'");	

// The table and checkboxes etc print out fine

 

When I echo $bf_issues_booked; I simply get the result 'Array'. Do I need to use implode instead or is this correct?

Link to comment
Share on other sites

$q = "SELECT `issues` FROM `magazine` WHERE `user_id` = '$user_id'";
$r = mysql_query( $q );

$data = mysql_result( $r, 0 );

$issues = explode( ',', $data );

for ( $i = 1; $i = $total_issues; $i++ ) {
echo	'<input type="checkbox" name="issues[]" value="'. $i .'"' .
	( in_array( $i, $issues ) : ' checked="checked"' : '' ) .
	'> Issue number '. $i .'<br />';
}

 

Then when they submit it, simply use

$data = implode( ',', $_POST['issues'] );
$q = "UPDATE `magazine` SET `issues` = '$data' WHERE `user_id` = '$user_id'";
mysql_query( $q );

Link to comment
Share on other sites

What you need to do is grab the comma seperated values then create an array from them I use a function for this to get the data. Loop through all your issues then check to see if the id is in the array that you created. use the in_array() command to see if is is in the array.

 

// Define the array variable

$bf_issues_booked = explode(",",$row['bf_issue_number']);

// just to see if there is any result

echo $bf_issues_booked;

// A loop used to populate my form with the checkboxes and issue name, number etc. This works fine

while($row=mysql_fetch_assoc($issues_result)) {
$selected = in_array($row['issue_number'], $bf_issues_booked) ? "selected" : "";

$issues[] = '<input name="issue_number[]" type="checkbox" value="' .$row['issue_number']. '" tabindex="11" $selected />Issue <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] ;

}

 

Ray

Link to comment
Share on other sites

I have pasted the code craygo but it selects all the checkboxes, not just the ones that are already in the comma separated array. Maybe I haven't explained myself correctly -

 

I have a query that selects all issues from the database within a 400 day period (This is needed because I need to display 13 issues) and echoes them to a table as a 2 column list. They all have unchecked checkboxes by default. What I need to do is compare these issue numbers with the comma separated array stored in the db to see if any are the same. If they are, I want to have the checkbox checked so that only the issues from the array are pre-selected - the user can then change select/de-select which ones they like.

 

Here is the code I have so far...

 

$issues_query="SELECT * FROM issues WHERE issue_on_sale BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 400 DAY)";

$issues_result=mysql_query($issues_query);

if(!$issues_result) {

echo("Query Error: ".mysql_error());

}

// Define the array variable

$bf_issues_booked = explode(",",$row['bf_issue_number']);


// A loop used to populate my form with the checkboxes and issue name, number etc. This works fine

while($row=mysql_fetch_assoc($issues_result)) {
$selected = in_array($row['issue_number'], $bf_issues_booked) ? "checked" : 'checked="yes"';

$issues[] = '<input name="issue_number[]" type="checkbox" value="' .$row['issue_number']. '" tabindex="11" ' . $selected . ' />Issue <strong>' .$row['issue_number'] . '</strong> ' . $row['issue_month'] . ' ' . $row['issue_year'] ;



}

// A function set up to poulate a table with values returned from the above loop
function issues_table($data, $details="")
{
$sret = "<table ".$details.">\n";

$all = count($data)-1;

for($i=0; $i <= $all; $i++)
{
	if(($i % 2)==0)
	{
		$sret .= "<tr><td>".$data[$i]."</td>";
	}
	else
	{
		$sret .= "<td>".$data[$i]."</td></tr>\n";
	}
}
//	Catch if an odd cell
if(($all % 2) == 0)
{
	$sret .= "<td><br></td></tr>\n";
}

$sret .= "</table>\n";
return $sret;
}

$data = $issues;

echo issues_table($data, "border='0' width='100%'");	

Link to comment
Share on other sites

I've managed to get this working if I make an array myself but not if I use the array (created using implode) from the db table so it looks like the problem is in my turning the db comma separated string into an array.

 

Here is the code I am using...

 

<?php
session_start();

include ('../config.php');
include ('../opendb.php');

if(!isset($_SESSION['Login']))

header ("location:../login.php");

$record = $_GET['id'];

$bf_record = $_GET['bf_id'];

$query="SELECT * FROM contacts, booking_form WHERE contacts_id = $record AND bf_id = $bf_record";
$result=mysql_query($query);
if(!$result) {
	print("Query Error: ".mysql_error());
}


$issues_query="SELECT * FROM issues WHERE issue_on_sale BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 400 DAY)";

$issues_result=mysql_query($issues_query);

if(!$issues_result) {

echo("Query Error: ".mysql_error());

}



$test = array(59,62,69,70,71,72);

$issues = implode(",",$row['bf_issues_booked']);

while($row = mysql_fetch_array($issues_result)) {

	echo '<li><input type="checkbox" value="' . $row['issue_number'] . '"'; if (in_array($row['issue_number'], implode(",",$row['bf_issues_booked']))) {

echo 'checked="yes">';

}

else {

echo'>';

}

echo '<strong>' . $row['issue_number'] . '</strong>' . ' ' . $row['issue_month'] . '</li>';
}

?>

 

If I use $test as the array I get this result...

 

checkboxes.png

 

But if I use the $issues array all checkboxes are blank.

 

What gives?

 

Link to comment
Share on other sites

ok couple things

 

you need to add this

//$query="SELECT * FROM contacts, booking_form WHERE contacts_id = $record AND bf_id = $bf_record";
$query="SELECT * FROM contacts WHERE contacts_id = $record";
$result=mysql_query($query);
if(!$result) {
	print("Query Error: ".mysql_error());
}
  $row = mysql_fetch_assoc($result); // add this line

 

Also need to change the variable for the issue loop

while($irow=mysql_fetch_assoc($issues_result)) {  // change this to $irow and the variables below
$selected = in_array($irow['issue_number'], $bf_issues_booked) ? "checked=\"checked\"" : "";


$issues[] = '<input name="issue_number[]" type="checkbox" value="' .$irow['issue_number']. '" tabindex="11" ' . $selected . ' />Issue <strong>' .$irow['issue_number'] . '</strong> ' . $irow['issue_month'] . ' ' . $irow['issue_year'] ;



}

 

That is what I found so far, let me know

 

Ray

 

Link to comment
Share on other sites

Still nothing gets selected until I change the array to the one i've created on the page instead of the one from the db.

 

I had to change...

 

$issues[] = '<input name="issue_number[]" type="checkbox" value="' .$irow['issue_number']. '" tabindex="11" ' . $selected . ' />Issue <strong>' .$irow['issue_number'] . '</strong> ' . $irow['issue_month'] . ' ' . $irow['issue_year'] ;

 

to...

 

echo '<li><input name="issue_number[]" type="checkbox" value="' .$irow['issue_number']. '" tabindex="11" ' . $selected . ' />Issue <strong>' .$irow['issue_number'] . '</strong> ' . $irow['issue_month'] . ' ' . $irow['issue_year'] . '</li>';

 

otherwise I would get a blank page?

 

Am I doing something wrong - does the array need to be echoed somewhere else??

 

I appreciate your help craygo!

Link to comment
Share on other sites

-- phpMyAdmin SQL Dump
-- version 2.10.2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Jul 09, 2008 at 04:30 PM
-- Server version: 5.0.41
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

-- 
-- Database: `customer_information`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `booking_form`
-- 

CREATE TABLE `booking_form` (
  `bf_id` int(6) NOT NULL auto_increment,
  `bf_company_id` int(6) NOT NULL,
  `bf_artwork_first_name` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_last_name` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_address` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_towncity` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_county` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_postcode` varchar(10) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_telephone` varchar(15) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_mobile` varchar(20) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_fax` varchar(20) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_email` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_advert_size` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_advert_position` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_copy_deadline` date NOT NULL,
  `bf_sales_contact` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_special_instructions` text collate utf8_unicode_ci NOT NULL,
  `bf_cost` decimal(10,2) NOT NULL,
  `bf_number_insertions` varchar(2) collate utf8_unicode_ci NOT NULL,
  `bf_issues_booked` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_special_position_premium` decimal(10,2) NOT NULL,
  `bf_pre_payment_discount` decimal(10,2) NOT NULL,
  `bf_total` decimal(10,2) NOT NULL,
  `bf_vat` decimal(10,2) NOT NULL,
  `bf_total_cost` decimal(10,2) NOT NULL,
  `bf_date_added` datetime NOT NULL,
  `bf_date_changed` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`bf_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=49 ;

-- --------------------------------------------------------

-- 
-- Table structure for table `contacts`
-- 

CREATE TABLE `contacts` (
  `contacts_id` int(6) NOT NULL auto_increment,
  `pre_payment` enum('Y','N') collate utf8_unicode_ci NOT NULL default 'N',
  `client_lead` enum('C','L') collate utf8_unicode_ci NOT NULL default 'C',
  `companyname` tinytext collate utf8_unicode_ci NOT NULL,
  `clienttype` varchar(30) collate utf8_unicode_ci NOT NULL,
  `section` varchar(30) collate utf8_unicode_ci NOT NULL,
  `businesstype` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactonefirstname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactonelastname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactoneemail` tinytext collate utf8_unicode_ci NOT NULL,
  `contacttwofirstname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contacttwolastname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contacttwoemail` varchar(50) collate utf8_unicode_ci NOT NULL,
  `website` varchar(30) collate utf8_unicode_ci NOT NULL,
  `phone` varchar(15) collate utf8_unicode_ci NOT NULL,
  `fax` varchar(20) collate utf8_unicode_ci NOT NULL,
  `mobile` varchar(20) collate utf8_unicode_ci NOT NULL,
  `address` varchar(30) collate utf8_unicode_ci NOT NULL,
  `towncity` varchar(30) collate utf8_unicode_ci NOT NULL,
  `countystate` varchar(30) collate utf8_unicode_ci NOT NULL,
  `postcodezip` varchar(10) collate utf8_unicode_ci NOT NULL,
  `country` varchar(30) collate utf8_unicode_ci NOT NULL,
  `notes` longtext collate utf8_unicode_ci NOT NULL,
  `followup` date NOT NULL,
  `dateadded` datetime NOT NULL,
  `datechanged` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`contacts_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=52 ;

-- --------------------------------------------------------

-- 
-- Table structure for table `issues`
-- 

CREATE TABLE `issues` (
  `issues_id` int(6) NOT NULL auto_increment,
  `issue_number` tinyint(3) NOT NULL,
  `issue_month` tinytext collate utf8_unicode_ci NOT NULL,
  `issue_year` smallint(4) NOT NULL,
  `issue_month_start` date NOT NULL,
  `issue_month_end` date NOT NULL,
  `issue_artwork_deadline` date NOT NULL,
  `issue_artwork_to_printers` date NOT NULL,
  `issue_back_from_printers` date NOT NULL,
  `issue_on_sale` date NOT NULL,
  PRIMARY KEY  (`issues_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=25 ;

Link to comment
Share on other sites

cool but could use some data from the tables. :)

 

Also are the tables linked in any way. Because your first query

$query="SELECT * FROM contacts, booking_form WHERE contacts_id = $record AND bf_id = $bf_record";

Doesn't really mean anything because there is no link between them. is the contact tables linked to the booking table??

Link to comment
Share on other sites

Craygo,

 

When you say are the tables linked, in what way do you mean? They are two separate tables  - the contacts holds all the contacts (i.e. people) and the booking_form holds the booking form information for the contacts. There will be a variable amount of booking form entries for each contact. Each one occupies its own row and there is a field in this table called bf_company_id which holds a copy of the individual contact_id (auto_increment) so that I can insert the booking forms and relate it to the correct contact. Is this what you mean?

 

Here is my MYSQL dump with sample data.

 

Regards

 

James

 

-- phpMyAdmin SQL Dump
-- version 2.10.2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Jul 10, 2008 at 04:22 PM
-- Server version: 5.0.41
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

-- 
-- Database: `customer_information`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `booking_form`
-- 

CREATE TABLE `booking_form` (
  `bf_id` int(6) NOT NULL auto_increment,
  `bf_company_id` int(6) NOT NULL,
  `bf_artwork_first_name` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_last_name` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_address` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_towncity` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_county` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_postcode` varchar(10) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_telephone` varchar(15) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_mobile` varchar(20) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_fax` varchar(20) collate utf8_unicode_ci NOT NULL,
  `bf_artwork_email` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_advert_size` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_advert_position` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_copy_deadline` date NOT NULL,
  `bf_sales_contact` varchar(30) collate utf8_unicode_ci NOT NULL,
  `bf_special_instructions` text collate utf8_unicode_ci NOT NULL,
  `bf_cost` decimal(10,2) NOT NULL,
  `bf_number_insertions` varchar(2) collate utf8_unicode_ci NOT NULL,
  `bf_issues_booked` tinytext collate utf8_unicode_ci NOT NULL,
  `bf_special_position_premium` decimal(10,2) NOT NULL,
  `bf_pre_payment_discount` decimal(10,2) NOT NULL,
  `bf_total` decimal(10,2) NOT NULL,
  `bf_vat` decimal(10,2) NOT NULL,
  `bf_total_cost` decimal(10,2) NOT NULL,
  `bf_date_added` datetime NOT NULL,
  `bf_date_changed` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`bf_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1236 ;

-- 
-- Dumping data for table `booking_form`
-- 

INSERT INTO `booking_form` VALUES (1234, 2, 'Test', 'Name', 'Test Address', 'Test Town', 'Test County', 'TES T12', '01568 568944', '01356 564656', '01589 653219', 'test@test.com', '1 x 3', 'LHS', '2008-10-10', 'Christina James', 'None...really!', 1500.00, '5', '59,60,61,62,63', 250.00, 0.00, 1750.00, 306.25, 2056.25, '2008-07-10 15:16:43', '2008-07-10 15:16:43');
INSERT INTO `booking_form` VALUES (1235, 2, 'Test', 'Name', 'Test Address', 'Test Town', 'Test County', 'TES T12', '01568 568944', '01356 564656', '01589 653219', 'test@test.com', '2 x 4', 'RHS', '2009-07-16', 'Lucinda Watkins', 'Second booking form test.', 800.00, '5', '69,70,71,72,73', 50.00, 150.00, 700.00, 122.50, 822.50, '2008-07-10 16:08:34', '2008-07-10 16:08:34');

-- --------------------------------------------------------

-- 
-- Table structure for table `contacts`
-- 

CREATE TABLE `contacts` (
  `contacts_id` int(6) NOT NULL auto_increment,
  `pre_payment` enum('Y','N') collate utf8_unicode_ci NOT NULL default 'N',
  `client_lead` enum('C','L') collate utf8_unicode_ci NOT NULL default 'C',
  `companyname` tinytext collate utf8_unicode_ci NOT NULL,
  `clienttype` varchar(30) collate utf8_unicode_ci NOT NULL,
  `section` varchar(30) collate utf8_unicode_ci NOT NULL,
  `businesstype` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactonefirstname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactonelastname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contactoneemail` tinytext collate utf8_unicode_ci NOT NULL,
  `contacttwofirstname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contacttwolastname` varchar(30) collate utf8_unicode_ci NOT NULL,
  `contacttwoemail` varchar(50) collate utf8_unicode_ci NOT NULL,
  `website` varchar(30) collate utf8_unicode_ci NOT NULL,
  `phone` varchar(15) collate utf8_unicode_ci NOT NULL,
  `fax` varchar(20) collate utf8_unicode_ci NOT NULL,
  `mobile` varchar(20) collate utf8_unicode_ci NOT NULL,
  `address` varchar(30) collate utf8_unicode_ci NOT NULL,
  `towncity` varchar(30) collate utf8_unicode_ci NOT NULL,
  `countystate` varchar(30) collate utf8_unicode_ci NOT NULL,
  `postcodezip` varchar(10) collate utf8_unicode_ci NOT NULL,
  `country` varchar(30) collate utf8_unicode_ci NOT NULL,
  `notes` longtext collate utf8_unicode_ci NOT NULL,
  `followup` date NOT NULL,
  `dateadded` datetime NOT NULL,
  `datechanged` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`contacts_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `contacts`
-- 

INSERT INTO `contacts` VALUES (1, '', '', 'Acme Bananas', 'Classified', 'Flowers', '', 'King', 'Kong', 'king@kong.com', '', '', '', 'www.kingkong.com', '01478 456897', '01689 458745', '07986 456879', '123 Primate Crescent', 'Simianville', 'Yorkshire', 'YO3 4DJ', 'United Kingdom', 'These bananas sure taste good!\r\n\r\nI love ''em!', '2009-07-09', '2008-07-10 15:03:02', '2008-07-10 15:03:02');
INSERT INTO `contacts` VALUES (2, 'N', 'L', 'The Simpsons', 'Display', 'Decoration', '', 'Homer', 'Simpson', 'homer@thesimpsons.com', 'Marge', 'Simpson', 'marge@thesimpsons.com', 'www.thesimpsons.com', '01659 568987', '01589 568974', '07856 326598', 'The Yellow House', 'Springfield', 'Missouri', 'SP1 456', 'United Kingdom', 'Doh!\r\nDoh!', '2008-07-25', '2008-07-10 15:11:17', '2008-07-10 15:11:17');

Link to comment
Share on other sites

A question about this topic. I am doing something similar

 

You are storing different 'issues' as such '1,15,21,32,55'

How would you do about searching for all the records that have the issue '15'.

 

You cannot just go, because your issues column is more then likely a varchar, so 15 <> '1,15,21,32,55'

SELECT * FROM table WHERE issues = '15'

 

So how would you go about searching for the all the users who have a certain issue?

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.